U
    ÓZj“  ã                   @   sÜ   d 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Zddl	Z
ddlmZ ddlmZ ddlmZmZ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„ dejƒZdd„ ZG dd„ dejƒZedkrØe ¡  dS )z%Tests for certbot.plugins.standalone.é    N©Úerrno)Ú
challenges)Ú
standalone)ÚDictÚTupleÚSet)Úachallenges)Úerrors)Ú	acme_util)Úutilc                   @   s@   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dS )ÚServerManagerTestz3Tests for certbot.plugins.standalone.ServerManager.c                 C   s,   ddl m} i | _i | _|| j| jƒ| _d S )Nr   )ÚServerManager)Úcertbot.plugins.standaloner   ÚcertsÚhttp_01_resourcesÚmgr)Úselfr   © r   úb/var/www/html/TRUCKING_PROJECT/venv/lib/python3.8/site-packages/certbot/plugins/standalone_test.pyÚsetUp   s    zServerManagerTest.setUpc                 C   s,   |   | jj| jk¡ |   | jj| jk¡ d S ©N)Ú
assertTruer   r   r   ©r   r   r   r   Ú	test_init"   s    ÿzServerManagerTest.test_initc                 C   sZ   | j jd|d�}| ¡ d d }|  | j  ¡ ||i¡ | j j|d� |  | j  ¡ i ¡ d S )Nr   ©ÚportÚchallenge_typeé   )r   )r   ÚrunÚgetsocknamesÚassertEqualÚrunningÚstop)r   r   Úserverr   r   r   r   Ú_test_run_stop'   s
    z ServerManagerTest._test_run_stopc                 C   s   |   tj¡ d S r   )r%   r   ÚHTTP01r   r   r   r   Útest_run_stop_http_01.   s    z'ServerManagerTest.test_run_stop_http_01c                 C   sz   | j jdtjd�}| ¡ d d }| j j|tjd�}|  | j  ¡ ||i¡ |  ||k¡ | j  |¡ |  | j  ¡ i ¡ d S )Nr   r   r   )	r   r   r   r&   r    r!   r"   r   r#   )r   r$   r   Úserver2r   r   r   Útest_run_idempotent1   s    z%ServerManagerTest.test_run_idempotentc                 C   s”   t   t j¡}| d¡ | ¡ d }t   ¡ }z| d|f¡ W n t jk
rR   Y nX | jtj| jj	|t
jd� |  | j ¡ i ¡ | ¡  | ¡  d S )N©Ú r   r   r+   )r   )ÚsocketÚAF_INET6ÚbindÚgetsocknameÚerrorÚassertRaisesr
   ÚStandaloneBindErrorr   r   r   r&   r!   r"   Úclose)r   Zsome_serverr   Zmaybe_another_serverr   r   r   Útest_run_bind_error:   s"    
  þz%ServerManagerTest.test_run_bind_errorN)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r%   r'   r)   r4   r   r   r   r   r      s   	r   c                  C   s4   t   t jt jd¡} |  d¡ |  ¡ d }|  ¡  |S )z%Gets an open port number from the OS.r   r*   r   )r,   ÚAF_INETÚSOCK_STREAMr.   r/   r3   )Zopen_socketr   r   r   r   Úget_open_portK   s
    
r;   c                   @   s„   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Ze 	¡ d
d„ ƒZ
e 	¡ dd„ ƒZdd„ Zdd„ Zdd„ Zdd„ Zedd„ ƒZdd„ ZdS )ÚAuthenticatorTestz3Tests for certbot.plugins.standalone.Authenticator.c                 C   s<   ddl m} tjtƒ d�| _|| jdd�| _t ¡ | j_d S )Nr   )ÚAuthenticator)Zhttp01_portr   )Úname)r   r=   ÚmockÚ	MagicMockr;   ÚconfigÚauthÚservers)r   r=   r   r   r   r   W   s    zAuthenticatorTest.setUpc                 C   s   |   t| j ¡ tjƒ¡ d S r   )r   Ú
isinstancerB   Z	more_infoÚsixÚstring_typesr   r   r   r   Útest_more_info^   s    z AuthenticatorTest.test_more_infoc                 C   s   |   | jjd d�tjg¡ d S )N)Údomain)r!   rB   Zget_chall_prefr   r&   r   r   r   r   Útest_get_chall_prefa   s    ÿz%AuthenticatorTest.test_get_chall_prefc                 C   s2   |   ¡ }| j |¡}dd„ |D ƒ}|  ||¡ d S )Nc                 S   s   g | ]}|  |j¡‘qS r   )ÚresponseÚaccount_key)Ú.0Zachallr   r   r   Ú
<listcomp>i   s     z2AuthenticatorTest.test_perform.<locals>.<listcomp>)Ú_get_achallsrB   Úperformr!   )r   ZachallsrJ   Úexpectedr   r   r   Útest_performe   s    zAuthenticatorTest.test_performc                 C   s^   |ƒ }t j}t tj|d�d¡}|gdt ¡ g  | jjj_	|j
}d|_|  ¡  |  |¡ d S )Nr   éÿÿÿÿé   T)Úsocket_errorsÚ
EADDRINUSEr
   r2   r?   r@   rB   rC   r   Úside_effectÚyesnoÚreturn_valuerQ   Ú_assert_correct_yesno_call)r   Úmock_get_utilityÚmock_utilityr   r0   Ú
mock_yesnor   r   r   Útest_perform_eaddrinuse_retryl   s    z/AuthenticatorTest.test_perform_eaddrinuse_retryc                 C   s8   |ƒ }|j }d|_tj}|  tj| j|¡ |  |¡ d S )NF)	rW   rX   rT   rU   r1   r
   ÚPluginErrorÚ_fail_performrY   )r   rZ   r[   r\   r   r   r   r   Ú test_perform_eaddrinuse_no_retryx   s    z2AuthenticatorTest.test_perform_eaddrinuse_no_retryc                 C   s2   |j \}}|  d|d k¡ |  | dd¡¡ d S )Nzin user   ÚdefaultT)Z	call_argsr   ZassertFalseÚget)r   r\   Z
yesno_argsZyesno_kwargsr   r   r   rY   ‚   s    
z,AuthenticatorTest._assert_correct_yesno_callc                 C   s   t j}|  tj| j|¡ d S r   )rT   ÚEACCESr1   r
   r^   r_   ©r   r   r   r   r   Útest_perform_eacces‡   s    z%AuthenticatorTest.test_perform_eaccesc                 C   s   t j}|  tj| j|¡ d S r   )rT   ÚENOTCONNr1   r
   r2   r_   rd   r   r   r   Ú$test_perform_unexpected_socket_error‹   s      ÿz6AuthenticatorTest.test_perform_unexpected_socket_errorc                 C   s4   t  tj|d�d¡}|| jjj_| j |  	¡ ¡ d S )Nr   rR   )
r
   r2   r?   r@   rB   rC   r   rV   rO   rN   )r   r   r0   r   r   r   r_   �   s    zAuthenticatorTest._fail_performc                 C   s.   d}t j t d¡¡}tjtj||d�}|gS )Ns	   localhostzrsa512_key.pem)ZchallbrH   rK   )	ÚjoseZJWKÚloadÚ	test_utilZload_vectorr	   Z"KeyAuthorizationAnnotatedChallenger   ZHTTP01_P)ÚclsrH   ÚkeyZhttp_01r   r   r   rN   •   s      ÿzAuthenticatorTest._get_achallsc                 C   s  dddœ| j jj_| j jd  d¡ | j jd  ddg¡ | j  dg¡ |  | j jt	ƒ t	ddgƒdœ¡ | j jj
 d¡ d	di| j jj_| j  dg¡ |  | j jt	ƒ t	dgƒdœ¡ |  d| j jj
j¡ | j  dg¡ |  | j jt	ƒ t	g ƒdœ¡ | j jj
 d	¡ d S )
NÚserver1r(   )r   rS   Zchall1Zchall2Zchall3)rm   r(   r   rS   )rB   rC   r"   rX   ZservedÚaddÚupdateÚcleanupr!   Úsetr#   Zassert_called_once_withZ
call_countZassert_called_withr   r   r   r   Útest_cleanupž   s4    þ
 
ÿ ÿ
 ÿ
 ÿzAuthenticatorTest.test_cleanupN)r5   r6   r7   r8   r   rG   rI   rQ   rj   Zpatch_get_utilityr]   r`   rY   re   rg   r_   ÚclassmethodrN   rr   r   r   r   r   r<   T   s    

	
r<   Ú__main__) r8   r,   r   rT   ZunittestZjosepyrh   r?   rE   ZOpenSSL.cryptoZOpenSSLZacmer   r   Zacme_standaloneZacme.magic_typingr   r   r   Zcertbotr	   r
   Zcertbot.testsr   r   rj   ZTestCaser   r;   r<   r5   Úmainr   r   r   r   Ú<module>   s&   3	e