B
    v9ag                 @   sN  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 yd dlZW n ek
rX   dZY nX 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 eedkd	G d
d dejZG dd dejZeedkd	G dd deejZeedkd	eeeddG dd deejZedkrJe  dS )    N)mock)log)	protocols)sslproto)tasks)utils)
functionalzNo ssl modulec                   s   e Zd Z fddZdddddZdddd	Zd
d Zdd Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Z  ZS )SslProtoHandshakeTestsc                s$   t    t | _| | j d S )N)supersetUpasyncioZnew_event_looploopZset_event_loop)self)	__class__ #/usr/lib/python3.7/test_sslproto.pyr      s    

zSslProtoHandshakeTests.setUpN)waiterprotoc            C   sR   t  }|d krt }tj| j|||dd}| |j	 | | 
|jj |S )Ng?)ssl_handshake_timeout)
test_utilsdummy_ssl_contextr   Protocolr   SSLProtocolr   assertIs_app_transportget_protocolZ
addCleanupclose)r   r   r   
sslcontext	ssl_protor   r   r   ssl_protocol   s    z#SslProtoHandshakeTests.ssl_protocol)do_handshakec         	   C   s^   t  }t  }d|j_|r&||j_ndd }||j_t jd|d || W d Q R X |S )N    c             S   s   g S )Nr   )callbackr   r   r   mock_handshake0   s    z>SslProtoHandshakeTests.connection_made.<locals>.mock_handshakezasyncio.sslproto._SSLPipe)return_value)r   Mockshutdownr$   r    Zside_effectZpatchconnection_made)r   r   r    	transportZsslpiper#   r   r   r   r'   )   s    
z&SslProtoHandshakeTests.connection_madec          	   C   sJ   t  }t }t }| td tj| j|||dd W d Q R X d S )Nza positive numberr   )r   )	r   r   r   r%   assertRaisesRegex
ValueErrorr   r   r   )r   r   	app_protor   r   r   r   test_handshake_timeout_zero7   s    z2SslProtoHandshakeTests.test_handshake_timeout_zeroc          	   C   sJ   t  }t }t }| td tj| j|||dd W d Q R X d S )Nza positive numberi)r   )	r   r   r   r%   r)   r*   r   r   r   )r   r   r+   r   r   r   r   test_handshake_timeout_negative?   s    z6SslProtoHandshakeTests.test_handshake_timeout_negativec             C   sL   t j| jd}| j|d}| | |  t| j | |	 t
 d S )N)r   )r   )r   Futurer   r   r'   eof_receivedr   run_brieflyassertIsInstance	exceptionConnectionResetError)r   r   r   r   r   r   test_eof_received_waiterG   s    
z/SslProtoHandshakeTests.test_eof_received_waiterc          	   C   sT   t j| jd}| j|d}tj }tjtj	 z|
d  W d tj| X d S )N)r   )r   )r   r.   r   r   r   ZloggerZgetEffectiveLevelZsetLevelloggingZFATALZ_fatal_error)r   r   r   Z	log_levelr   r   r   test_fatal_error_no_name_errorO   s    
z5SslProtoHandshakeTests.test_fatal_error_no_name_errorc             C   sN   t j| jd}| j|d}| | |t t| j | 	|
 t d S )N)r   )r   )r   r.   r   r   r'   connection_lostConnectionAbortedErrorr   r0   r1   r2   )r   r   r   r   r   r   test_connection_lost^   s    

z+SslProtoHandshakeTests.test_connection_lostc             C   sL   t j| jd}| j|d}| |}t| j |j  | 	|j
j d S )N)r   )r   )r   r.   r   r   r'   r   r0   r   r   
assertTrueabortZcalled)r   r   r   r(   r   r   r   test_close_during_handshakeh   s    

z2SslProtoHandshakeTests.test_close_during_handshakec             C   s|   t j| jd}| j|d}| |d t }| |d|| | | | 	|d |
d  | |d d S )N)r   )r   socket)r   r.   r   r   assertIsNoneZ_get_extra_infoobjectr   r'   ZassertIsNotNoner7   )r   r   r   defaultr   r   r   (test_get_extra_info_on_closed_connections   s    

z?SslProtoHandshakeTests.test_get_extra_info_on_closed_connectionc             C   sR   t j| jd}| j|d}t  }|j| | |j | | |j	| d S )N)r   )r   )
r   r.   r   r   r   r   set_protocolr   r   Z_app_protocol)r   r   r   Znew_app_protor   r   r   test_set_new_app_protocol~   s    z0SslProtoHandshakeTests.test_set_new_app_protocolc             C   s4   |   }| | |j}|  | |d d S )Ns   data)r   r'   r   r   r>   data_received)r   r   transpr   r   r    test_data_received_after_closing   s
    
z7SslProtoHandshakeTests.test_data_received_after_closingc             C   s4   |   }| | |j}|  | |d d S )Ns   data)r   r'   r   r   r>   write)r   r   rE   r   r   r   test_write_after_closing   s
    
z/SslProtoHandshakeTests.test_write_after_closing)__name__
__module____qualname__r   r   r'   r,   r-   r4   r6   r9   r<   rA   rC   rF   rH   __classcell__r   r   )r   r   r	      s   


r	   c               @   st   e Zd ZdZdZdd Zdd Zdd Zd	d
 Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd ZdS )BaseStartTLSi  <   c             C   s   t d S )N)NotImplementedError)r   r   r   r   new_loop   s    zBaseStartTLS.new_loopc          
   C   s  G dd dt j}xdD ]}|d|}t|d | |jd |d|}t|d | |jd |d|}t|d | |jd |d|}t|d | |jd |d	|}t|d | |jd |d
|}| td t|d W d Q R X qW d S )Nc               @   s$   e Zd Zdd Zdd Zdd ZdS )z.BaseStartTLS.test_buf_feed_data.<locals>.Protoc             S   s&   t || _t| j| _d| _|| _d S )Nr!   )	bytearraybuf
memoryviewmvdatausemv)r   bufsizerV   r   r   r   __init__   s    
z7BaseStartTLS.test_buf_feed_data.<locals>.Proto.__init__c             S   s   | j r| jS | jS d S )N)rV   rT   rR   )r   sizehintr   r   r   
get_buffer   s    z9BaseStartTLS.test_buf_feed_data.<locals>.Proto.get_bufferc             S   s<   | j r |  j| jd | 7  _n|  j| jd | 7  _d S )N)rV   rU   rT   rR   )r   nsizer   r   r   buffer_updated   s    z=BaseStartTLS.test_buf_feed_data.<locals>.Proto.buffer_updatedN)rI   rJ   rK   rX   rZ   r\   r   r   r   r   Proto   s   r]   )FT   s   12345   s   1234   d   r   zempty buffer)r   BufferedProtocolr   Z_feed_data_to_buffered_protoassertEqualrU   r)   RuntimeError)r   r]   rV   r   r   r   r   test_buf_feed_data   s(    






zBaseStartTLS.test_buf_feed_datac          	      s   dj  t t fdd}G fdddtj  fdd}j|jd&}j	tj
||jjd	d
 W d Q R X t  d S )N   1c                s   |  j | t }t|t  | jdd | d | t }t|t  | tj	 | 
  d S )NT)server_side   O)
settimeoutTIMEOUTrecv_alllenrc   	start_tlssendallr&   r=   	SHUT_RDWRr   )sockrU   )	HELLO_MSGr   server_contextr   r   serve   s    
z=BaseStartTLS.test_start_tls_client_reg_proto_1.<locals>.servec                   s0   e Zd Zdd Z fddZdd Zdd Zd	S )
zCBaseStartTLS.test_start_tls_client_reg_proto_1.<locals>.ClientProtoc             S   s   || _ || _d| _d S )Nr   )on_dataon_eofcon_made_cnt)r   rt   ru   r   r   r   rX      s    zLBaseStartTLS.test_start_tls_client_reg_proto_1.<locals>.ClientProto.__init__c                s    |  j d7  _  | j d d S )Nr^   )rv   rc   )r   tr)r   r   r   r'      s    zSBaseStartTLS.test_start_tls_client_reg_proto_1.<locals>.ClientProto.connection_madec             S   s   | j | d S )N)rt   
set_result)r   rU   r   r   r   rD      s    zQBaseStartTLS.test_start_tls_client_reg_proto_1.<locals>.ClientProto.data_receivedc             S   s   | j d d S )NT)ru   rx   )r   r   r   r   r/      s    zPBaseStartTLS.test_start_tls_client_reg_proto_1.<locals>.ClientProto.eof_receivedN)rI   rJ   rK   rX   r'   rD   r/   r   )r   r   r   ClientProto   s   ry   c                s   t jdjdI d H  j  j jj fddf|  I d H \}}| j||I d H } I d H d | I d H  |  d S )Ng      ?)r   c                  s
    S )Nr   r   )ry   rt   ru   r   r   <lambda>  r!   zPBaseStartTLS.test_start_tls_client_reg_proto_1.<locals>.client.<locals>.<lambda>rh   )	r   sleepr   create_futurecreate_connectionrG   rm   rc   r   )addrrw   r   new_tr)ry   rq   client_contextr   )rt   ru   r   client   s    

"


z>BaseStartTLS.test_start_tls_client_reg_proto_1.<locals>.client)timeout
   )r   r   )PAYLOAD_SIZEr   simple_server_sslcontextsimple_client_sslcontextr   r   
tcp_serverrj   r   run_until_completewait_forr~   weakrefrefr>   )r   rs   r   srvr   )ry   rq   r   r   rr   r   !test_start_tls_client_reg_proto_1   s    
$
z.BaseStartTLS.test_start_tls_client_reg_proto_1c          	      s   dj  t t fdd}G fdddtj  fdd}j|jd"}j	tj
||jd	d W d Q R X t  d S )
Nrf   c                s^   |  j | jdd | d | t }t|t  | tj	 | 
  d S )NT)rg   rh   )ri   rj   rm   rn   rk   rl   rc   r&   r=   ro   r   )rp   rU   )rq   r   rr   r   r   rs     s    
z>BaseStartTLS.test_create_connection_memory_leak.<locals>.servec                   s0   e Zd Zdd Z fddZdd Zdd Zd	S )
zDBaseStartTLS.test_create_connection_memory_leak.<locals>.ClientProtoc             S   s   || _ || _d| _d S )Nr   )rt   ru   rv   )r   rt   ru   r   r   r   rX   )  s    zMBaseStartTLS.test_create_connection_memory_leak.<locals>.ClientProto.__init__c                s&   || _ |  jd7  _ | jd d S )Nr^   )rw   rv   rc   )r   rw   )r   r   r   r'   .  s    zTBaseStartTLS.test_create_connection_memory_leak.<locals>.ClientProto.connection_madec             S   s   | j | d S )N)rt   rx   )r   rU   r   r   r   rD   5  s    zRBaseStartTLS.test_create_connection_memory_leak.<locals>.ClientProto.data_receivedc             S   s   | j d d S )NT)ru   rx   )r   r   r   r   r/   8  s    zQBaseStartTLS.test_create_connection_memory_leak.<locals>.ClientProto.eof_receivedN)rI   rJ   rK   rX   r'   rD   r/   r   )r   r   r   ry   (  s   ry   c                s   t dI d H  j  j jj fddf| diI d H \}} I d H d | I d H  |  d S )Ng      ?c                  s
    S )Nr   r   )ry   rt   ru   r   r   rz   B  r!   zQBaseStartTLS.test_create_connection_memory_leak.<locals>.client.<locals>.<lambda>sslrh   )r   r{   r   r|   r}   rc   rG   r   )r~   rw   r   )ry   rq   r   r   )rt   ru   r   r   ;  s    



z?BaseStartTLS.test_create_connection_memory_leak.<locals>.client)r   r   )r   r   r   r   r   r   r   rj   r   r   r   r~   r   r   r>   )r   rs   r   r   r   )ry   rq   r   r   rr   r   "test_create_connection_memory_leak  s    
 
z/BaseStartTLS.test_create_connection_memory_leakc          	      s   dj  t t dfdd}G fdddtj G fdddtj fd	d
}j|jd(}j	
tj||jj	jd W d Q R X d S )Nrf   r   c                s   |  j | t }t|t  | jdd | d | t }t|t  | d | t }t|t  | tj	 | 
  d S )NT)rg   rh      2)ri   rj   rk   rl   rc   rm   rn   r&   r=   ro   r   )rp   rU   )rq   r   rr   r   r   rs   [  s    

z=BaseStartTLS.test_start_tls_client_buf_proto_1.<locals>.servec                   s0   e Zd Zdd Z fddZdd Zdd Zd	S )
zHBaseStartTLS.test_start_tls_client_buf_proto_1.<locals>.ClientProtoFirstc             S   s   || _ td| _d S )Nr^   )rt   rQ   rR   )r   rt   r   r   r   rX   o  s    zQBaseStartTLS.test_start_tls_client_buf_proto_1.<locals>.ClientProtoFirst.__init__c                s    d7  d S )Nr^   r   )r   rw   )client_con_made_callsr   r   r'   s  s    zXBaseStartTLS.test_start_tls_client_buf_proto_1.<locals>.ClientProtoFirst.connection_madec             S   s   | j S )N)rR   )r   rY   r   r   r   rZ   w  s    zSBaseStartTLS.test_start_tls_client_buf_proto_1.<locals>.ClientProtoFirst.get_bufferc             S   s   | j t| jd |  d S )N)rt   rx   bytesrR   )r   r[   r   r   r   r\   z  s    zWBaseStartTLS.test_start_tls_client_buf_proto_1.<locals>.ClientProtoFirst.buffer_updatedN)rI   rJ   rK   rX   r'   rZ   r\   r   )r   r   r   ClientProtoFirstn  s   r   c                   s0   e Zd Zdd Z fddZdd Zdd Zd	S )
zIBaseStartTLS.test_start_tls_client_buf_proto_1.<locals>.ClientProtoSecondc             S   s   || _ || _d| _d S )Nr   )rt   ru   rv   )r   rt   ru   r   r   r   rX     s    zRBaseStartTLS.test_start_tls_client_buf_proto_1.<locals>.ClientProtoSecond.__init__c                s    d7  d S )Nr^   r   )r   rw   )r   r   r   r'     s    zYBaseStartTLS.test_start_tls_client_buf_proto_1.<locals>.ClientProtoSecond.connection_madec             S   s   | j | d S )N)rt   rx   )r   rU   r   r   r   rD     s    zWBaseStartTLS.test_start_tls_client_buf_proto_1.<locals>.ClientProtoSecond.data_receivedc             S   s   | j d d S )NT)ru   rx   )r   r   r   r   r/     s    zVBaseStartTLS.test_start_tls_client_buf_proto_1.<locals>.ClientProtoSecond.eof_receivedN)rI   rJ   rK   rX   r'   rD   r/   r   )r   r   r   ClientProtoSecond~  s   r   c                s   t jdjdI d H  j  j }j }jj fddf|  I d H \}}| j||I d H } I d H d | ||| |I d H d | |I d H  |	  d d S )Ng      ?)r   c                  s    S )Nr   r   )r   on_data1r   r   rz     r!   zPBaseStartTLS.test_start_tls_client_buf_proto_1.<locals>.client.<locals>.<lambda>rh   r   r^   )
r   r{   r   r|   r}   rG   rm   rc   rB   r   )r~   Zon_data2ru   rw   r   r   )r   r   rq   r   r   r   )r   r   r     s     


 



z>BaseStartTLS.test_start_tls_client_buf_proto_1.<locals>.client)r   )r   r   )r   r   r   r   r   rb   r   r   rj   r   r   r   r~   )r   rs   r   r   r   )r   r   rq   r   r   r   rr   r   !test_start_tls_client_buf_proto_1T  s    
z.BaseStartTLS.test_start_tls_client_buf_proto_1c          	      s   dj  t j fdd}G fdddtj  fdd}j|jd&}j	tj
||jjd	d
 W d Q R X d S )Nrf   c                sv   |  j | t }t|t  z8yjjd  | d}W n tk
rb   Y nX W d | 	  X d S )Ni   )
ri   rj   rk   rl   rc   r   Zcall_soon_threadsaferx   r8   r   )rp   rU   )rq   r   server_waits_on_handshaker   r   rs     s    

z=BaseStartTLS.test_start_tls_slow_client_cancel.<locals>.servec                   s0   e Zd Zdd Z fddZdd Zdd Zd	S )
zCBaseStartTLS.test_start_tls_slow_client_cancel.<locals>.ClientProtoc             S   s   || _ || _d| _d S )Nr   )rt   ru   rv   )r   rt   ru   r   r   r   rX     s    zLBaseStartTLS.test_start_tls_slow_client_cancel.<locals>.ClientProto.__init__c                s    |  j d7  _  | j d d S )Nr^   )rv   rc   )r   rw   )r   r   r   r'     s    zSBaseStartTLS.test_start_tls_slow_client_cancel.<locals>.ClientProto.connection_madec             S   s   | j | d S )N)rt   rx   )r   rU   r   r   r   rD     s    zQBaseStartTLS.test_start_tls_slow_client_cancel.<locals>.ClientProto.data_receivedc             S   s   | j d d S )NT)ru   rx   )r   r   r   r   r/     s    zPBaseStartTLS.test_start_tls_slow_client_cancel.<locals>.ClientProto.eof_receivedN)rI   rJ   rK   rX   r'   rD   r/   r   )r   r   r   ry     s   ry   c          	      s   t jdjdI d H  j  j jj fddf|  I d H \}}| I d H  t j* t jj	||djdI d H  W d Q R X d S )Ng      ?)r   c                  s
    S )Nr   r   )ry   rt   ru   r   r   rz     r!   zPBaseStartTLS.test_start_tls_slow_client_cancel.<locals>.client.<locals>.<lambda>)
r   r{   r   r|   r}   rG   assertRaisesTimeoutErrorr   rm   )r~   rw   r   )ry   rq   r   r   r   )rt   ru   r   r     s    

"

z>BaseStartTLS.test_start_tls_slow_client_cancel.<locals>.client)r   r   )r   r   )r   r   r   r   r|   r   r   r   rj   r   r   r~   )r   rs   r   r   r   )ry   rq   r   r   r   r   !test_start_tls_slow_client_cancel  s    

z.BaseStartTLS.test_start_tls_slow_client_cancelc                s   dj  d t t tjdsBtjdsBtjdrR jtj	O  _d  fddG  fdd	d	t
jfd
d fdd}j|  d S )Nrf   s   answerZfreebsdwindarwinc                sf   |  j | | | t}t|t |  |  | t |   d S )N)	ri   rj   Zconnectrk   rl   rc   rm   rn   r   )rp   r~   rU   )ANSWERrq   answerr   r   r   r   r     s    


z4BaseStartTLS.test_start_tls_server_1.<locals>.clientc                   s:   e Zd Zdd Zdd Zdd Z fddZd	d
 ZdS )z9BaseStartTLS.test_start_tls_server_1.<locals>.ServerProtoc             S   s   || _ || _d| _d | _d S )Nr!   )on_conon_con_lostrU   r(   )r   r   r   r   r   r   rX   
  s    zBBaseStartTLS.test_start_tls_server_1.<locals>.ServerProto.__init__c             S   s   || _ | j| d S )N)r(   r   rx   )r   rw   r   r   r   r'     s    zIBaseStartTLS.test_start_tls_server_1.<locals>.ServerProto.connection_madec             S   s
   || _ d S )N)r(   )r   rw   r   r   r   replace_transport  s    zKBaseStartTLS.test_start_tls_server_1.<locals>.ServerProto.replace_transportc                s0   |  j |7  _ t| j tkr,| j  d S )N)rU   rl   r(   rG   )r   rU   )r   rq   r   r   rD     s    zGBaseStartTLS.test_start_tls_server_1.<locals>.ServerProto.data_receivedc             S   s,   d | _ |d kr| jd  n| j| d S )N)r(   r   rx   Zset_exception)r   excr   r   r   r7     s    zIBaseStartTLS.test_start_tls_server_1.<locals>.ServerProto.connection_lostN)rI   rJ   rK   rX   r'   r   rD   r7   r   )r   rq   r   r   ServerProto	  s
   r   c                sn   |I d H }|   | jd jj|| djdI d H }| | |I d H  | j  |  d S )Nr!   T)rg   r   )rG   rc   rU   r   rm   rj   r   r   )r   r   r   rw   r   )rq   r   rr   r   r   main#  s    



z2BaseStartTLS.test_start_tls_server_1.<locals>.mainc           	      s   j  } j  }| |j fddddI d H }|jd   j fddjd( tj| |j jdI d H  W d Q R X |	  |
 I d H   d S )Nc                  s    S )Nr   r   )r   r   r   rz   :  r!   zHBaseStartTLS.test_start_tls_server_1.<locals>.run_main.<locals>.<lambda>z	127.0.0.1r   c                s
   |  S )Nr   )rp   )r~   r   r   r   rz   =  r!   )r   )r   r   )r   r|   Zcreate_serverZsocketsZgetsocknameZ
tcp_clientrj   r   r   r   Zwait_closedrc   )r   r   server)r   r   r   r   r   r   )r~   r   r   run_main4  s    



z6BaseStartTLS.test_start_tls_server_1.<locals>.run_main)r   r   r   r   sysplatform
startswithoptionsr   ZOP_NO_TLSv1_3r   r   r   r   )r   r   r   )	r   rq   r   r   r   r   r   r   rr   r   test_start_tls_server_1  s    
z$BaseStartTLS.test_start_tls_server_1c                s    fdd} j |  d S )Nc           	      sh     td  jd d d I d H  W d Q R X t }   td  jd d | I d H  W d Q R X d S )NzSSLContext, gotzis not supported)r)   	TypeErrorr   rm   r   r   )sslctx)r   r   r   r   J  s
     z4BaseStartTLS.test_start_tls_wrong_args.<locals>.main)r   r   )r   r   r   )r   r   test_start_tls_wrong_argsI  s    z&BaseStartTLS.test_start_tls_wrong_argsc          
      s   t   g jfdd dfdd} fdd}j|ddd	0}tj j||j	 W d Q R X W d Q R X 
 g  t     d S )
Nc                s
     |S )N)append)r   ctx)messagesr   r   rz   Z  r!   z5BaseStartTLS.test_handshake_timeout.<locals>.<lambda>Fc                s<   z,y|  d W n tk
r(   d Y nX W d |   X d S )Ni   T)rk   r8   r   )rp   )server_side_abortedr   r   r   ^  s
    z3BaseStartTLS.test_handshake_timeout.<locals>.serverc                s6   t jjjt jf|  ddddjdI d H  d S )N g      $@)r   server_hostnamer   g      ?)r   )r   r   r   r}   r   )r~   )client_sslctxr   r   r   r   g  s    z3BaseStartTLS.test_handshake_timeout.<locals>.clientr^   )max_clientsbacklog)r   r   r   set_exception_handlerr   r   r   r   r   r~   r:   rc   r   r   r>   )r   r   r   r   r   )r   r   r   r   r   test_handshake_timeoutT  s    	
&

z#BaseStartTLS.test_handshake_timeoutc          
      s   t   g jfdd dd } fdd}j|ddd0}td	 j||j W d Q R X W d Q R X 	g  d S )
Nc                s
     |S )N)r   )r   r   )r   r   r   rz     r!   zHBaseStartTLS.test_create_connection_ssl_slow_handshake.<locals>.<lambda>c             S   s8   z(y|  d W n tk
r$   Y nX W d |   X d S )Ni   )rk   r8   r   )rp   r   r   r   r     s
    
zFBaseStartTLS.test_create_connection_ssl_slow_handshake.<locals>.serverc                s&   t j|  djddI d H \}}d S )Nr   g      ?)r   r   r   r   )r   open_connectionr   )r~   readerwriter)r   r   r   r   r     s    zFBaseStartTLS.test_create_connection_ssl_slow_handshake.<locals>.clientr^   )r   r   zSSL handshake.*is taking longer)
r   r   r   r   r   r)   r8   r   r~   rc   )r   r   r   r   r   )r   r   r   r   )test_create_connection_ssl_slow_handshake  s    
&z6BaseStartTLS.test_create_connection_ssl_slow_handshakec          
      s   j dd  t tjdd fdd} fdd}j|d	d	d
0}tj j 	||j
 W d Q R X W d Q R X d S )Nc             S   s   d S )Nr   )r   r   r   r   r   rz     r!   zLBaseStartTLS.test_create_connection_ssl_failed_certificate.<locals>.<lambda>F)Zdisable_verifyc                sP   z@y| j  dd W n( tjk
r*   Y n tk
r<   Y nX W d |   X d S )NT)rg   )rm   r   SSLErrorOSErrorr   )rp   )r   r   r   r     s    
zJBaseStartTLS.test_create_connection_ssl_failed_certificate.<locals>.serverc                s&   t j|  djddI d H \}}d S )Nr   g      ?)r   r   r   r   )r   r   r   )r~   r   r   )r   r   r   r   r     s    zJBaseStartTLS.test_create_connection_ssl_failed_certificate.<locals>.clientr^   )r   r   )r   r   r   r   r   r   r   r   ZSSLCertVerificationErrorr   r~   )r   r   r   r   r   )r   r   r   r   -test_create_connection_ssl_failed_certificate  s    
z:BaseStartTLS.test_create_connection_ssl_failed_certificatec          	      sx   j dd  t t  fdd} fdd}j|ddd}j ||j}W d Q R X |d	 d S )
Nc             S   s   d S )Nr   )r   r   r   r   r   rz     r!   zBBaseStartTLS.test_start_tls_client_corrupted_ssl.<locals>.<lambda>c                sl   |   }zLy0| j dd | d | d |d W n tjk
rP   Y nX W d |  |   X d S )NT)rg   s   A
r^   s!   please corrupt the SSL connection)duprm   rn   rk   sendr   r   r   )rp   Z	orig_sock)r   r   r   r     s    


z@BaseStartTLS.test_start_tls_client_corrupted_ssl.<locals>.serverc          	      sr   t j|  djdI d H \}}| I d H d |d tj | I d H  W d Q R X |	  dS )Nr   )r   r   r   s   A
   BOK)
r   r   r   rc   readlinerG   r   r   r   r   )r~   r   r   )r   r   r   r   r     s    
z@BaseStartTLS.test_start_tls_client_corrupted_ssl.<locals>.clientr^   )r   r   r   )	r   r   r   r   r   r   r   r~   rc   )r   r   r   r   Zresr   )r   r   r   r   #test_start_tls_client_corrupted_ssl  s    
z0BaseStartTLS.test_start_tls_client_corrupted_sslN)rI   rJ   rK   r   rj   rP   re   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rM      s   /@>[=]0""rM   c               @   s   e Zd Zdd ZdS )SelectorStartTLSTestsc             C   s   t  S )N)r   ZSelectorEventLoop)r   r   r   r   rP     s    zSelectorStartTLSTests.new_loopN)rI   rJ   rK   rP   r   r   r   r   r     s   r   ProactorEventLoopzWindows onlyc               @   s   e Zd Zdd ZdS )ProactorStartTLSTestsc             C   s   t  S )N)r   r   )r   r   r   r   rP      s    zProactorStartTLSTests.new_loopN)rI   rJ   rK   rP   r   r   r   r   r     s   r   __main__)r5   r=   r   Zunittestr   r   r   ImportErrorr   r   r   r   r   Ztest.test_asyncior   r   r   Z
func_testsZskipIfZTestCaser	   ZFunctionalTestCaseMixinrM   r   Z
skipUnlesshasattrr   rI   r   r   r   r   r   <module>   s>   

     Z
