B
    u9ay  ã               @   sx   d dl Z d dlmZ d dlZd dlZe d¡Ze d¡ dd„ ZG dd„ de j	ƒZ
G d	d
„ d
e j	ƒZedkrte  ¡  dS )é    N)ÚsupportÚsslZnetworkc          	   C   sZ   t  ¡ }t | |f¡:}y|j|| d}W n tk
r>   dS X | ¡  dS W d Q R X d S )N)Zserver_hostnameFT)r   Úcreate_default_contextÚsocketZcreate_connectionZwrap_socketÚ	ExceptionÚclose)ZhostZportÚcontextZsock© r	   ú"/usr/lib/python3.7/test_smtpnet.pyÚcheck_ssl_verifiy
   s    r   c               @   s   e Zd ZdZdZdd„ ZdS )ÚSmtpTestzsmtp.gmail.comiK  c             C   sº   t  td¡ t tj¡}d|_tj|_t  	| j
¡~ t | j
| j¡}y|j|d W nF tjk
rš } z&|jd dkrˆt |jd ¡ n‚ W d d }~X Y nX | ¡  | ¡  W d Q R X d S )NÚSMTP_SSLF)r   r   z+STARTTLS extension not supported by server.)r   Úget_attributeÚsmtplibr   Ú
SSLContextÚPROTOCOL_TLS_CLIENTÚcheck_hostnameÚ	CERT_NONEÚverify_modeÚtransient_internetÚ
testServerZSMTPÚ
remotePortZstarttlsZSMTPExceptionÚargsÚunittestÚskipÚehloÚquit)Úselfr   ÚserverÚer	   r	   r
   Útest_connect_starttls   s    zSmtpTest.test_connect_starttlsN)Ú__name__Ú
__module__Ú__qualname__r   r   r    r	   r	   r	   r
   r      s   r   c               @   s4   e Zd ZdZdZdd„ Zdd„ Zdd„ Zd	d
„ ZdS )ÚSmtpSSLTestzsmtp.gmail.comiÑ  c          	   C   sH   t  td¡ t  | j¡& t | j| j¡}| ¡  | ¡  W d Q R X d S )Nr   )	r   r   r   r   r   r   r   r   r   )r   r   r	   r	   r
   Útest_connect0   s
    zSmtpSSLTest.test_connectc          	   C   sD   t  td¡ t  | j¡" t | j¡}| ¡  | ¡  W d Q R X d S )Nr   )r   r   r   r   r   r   r   r   )r   r   r	   r	   r
   Útest_connect_default_port7   s
    z%SmtpSSLTest.test_connect_default_portc          	   C   sf   t  t j¡}d|_t j|_t td¡ t 	| j
¡* tj| j
| j|d}| ¡  | ¡  W d Q R X d S )NFr   )r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r	   r	   r
   Útest_connect_using_sslcontext>   s    z)SmtpSSLTest.test_connect_using_sslcontextc          	   C   sˆ   t  | j¡" t| j| jƒ}|s*|  d¡ W d Q R X t  td¡ t 	¡ }t  | j¡* tj
| j| j|d}| ¡  | ¡  W d Q R X d S )Nz!SSL certificate can't be verifiedr   )r   )r   r   r   r   r   ZskipTestr   r   r   r   r   r   r   )r   Z
can_verifyr   r   r	   r	   r
   Ú&test_connect_using_sslcontext_verifiedH   s    z2SmtpSSLTest.test_connect_using_sslcontext_verifiedN)	r!   r"   r#   r   r   r%   r&   r'   r(   r	   r	   r	   r
   r$   ,   s   
r$   Ú__main__)r   Ztestr   r   r   Úimport_moduler   Zrequiresr   ZTestCaser   r$   r!   Úmainr	   r	   r	   r
   Ú<module>   s   

*