a
    zeQ                     @   s  d dl 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 d dl	m
Z
 d dlmZ d dlmZ zd dlZW n ey   dZY n0 dd	 ZG d
d de jZG dd de jZG dd dZG dd deZG dd deZG dd deeZejdkr>G dd deejZG dd deejZn|d dlZeedrfG dd deejZ eedrG dd  d eejZ!eed!rG d"d# d#eejZ"G d$d deejZe#d%kre$  dS )&    N)base_events)	constants)mock)support)socket_helper)utilsc                   C   s   t d  d S N)asyncioZset_event_loop_policy r
   r
   5/usr/lib/python3.9/test/test_asyncio/test_sendfile.pytearDownModule   s    r   c                       s>   e Zd ZdddZdd Zdd Zd	d
 Z fddZ  ZS )MySendfileProtoNr   c                 C   s@   d | _ d| _d| _|d ur.| | _| | _t | _|| _d S )NZINITIALr   )		transportstatenbytescreate_future	connecteddone	bytearraydataclose_after)selfloopr   r
   r
   r   __init__   s    

zMySendfileProto.__init__c                 C   s"   || _ d| _| jr| jd  d S )NZ	CONNECTED)r   r   r   
set_resultr   r   r
   r
   r   connection_made&   s    zMySendfileProto.connection_madec                 C   s
   d| _ d S )NZEOF)r   r   r
   r
   r   eof_received-   s    zMySendfileProto.eof_receivedc                 C   s   d| _ | jr| jd  d S )NZCLOSED)r   r   r   r   excr
   r
   r   connection_lost1   s    zMySendfileProto.connection_lostc                    sJ   |  j t|7  _ | j| t | | jrF| j | jkrF| j  d S r   )	r   lenr   extendsuperdata_receivedr   r   closer   r   	__class__r
   r   r%   7   s
    zMySendfileProto.data_received)Nr   )	__name__
__module____qualname__r   r   r   r!   r%   __classcell__r
   r
   r(   r   r      s
   

r   c                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )MyProtoc                 C   s(   d| _ d| _t | _| | _d | _d S NF)startedclosedr   r   r   futr   )r   r   r
   r
   r   r   B   s
    
zMyProto.__init__c                 C   s   d| _ || _d S NT)r0   r   r   r
   r
   r   r   I   s    zMyProto.connection_madec                 C   s   | j | d S r   )r   r#   r'   r
   r
   r   r%   M   s    zMyProto.data_receivedc                 C   s   d| _ | jd  d S r3   )r1   r2   r   r   r
   r
   r   r!   P   s    zMyProto.connection_lostc                    s   | j I d H  d S r   )r2   r   r
   r
   r   wait_closedT   s    zMyProto.wait_closedN)r*   r+   r,   r   r   r%   r!   r4   r
   r
   r
   r   r.   @   s
   r.   c                       sd   e Zd Zdd ZdZdd Ze fddZe fdd	Z fd
dZ	 fddZ
dd Z  ZS )SendfileBase   xi     c                 C   s   t d S r   )NotImplementedErrorr   r
   r
   r   create_event_loopd   s    zSendfileBase.create_event_loopc                    sF   t tjd}|| j W d    n1 s.0    Y  t   d S )Nwb)openr   TESTFNwriteDATAr$   
setUpClass)clsfpr(   r
   r   r?   g   s    *zSendfileBase.setUpClassc                    s   t t j t   d S r   )r   unlinkr<   r$   tearDownClassr@   r(   r
   r   rC   m   s    zSendfileBase.tearDownClassc                    s@   t tjd| _| | jj |  | _| | j t	 
  d S )Nrb)r;   r   r<   file
addCleanupr&   r9   r   Zset_event_loopr$   setUpr   r(   r
   r   rH   r   s
    
zSendfileBase.setUpc                    s4   | j  st| j  |   t  t   d S r   )	r   Z	is_closed
test_utilsZrun_brieflyZ
doCleanupsr   
gc_collectr$   tearDownr   r(   r
   r   rK   y   s
    
zSendfileBase.tearDownc                 C   s   | j |S r   )r   run_until_complete)r   coror
   r
   r   run_loop   s    zSendfileBase.run_loop)r*   r+   r,   r>   BUF_SIZEr9   classmethodr?   rC   rH   rK   rN   r-   r
   r
   r(   r   r5   X   s   	r5   c                       st   e Zd Ze fddZe fddZdddZdd	 ZdddZdd Z	dd Z
dd Zdd Zdd Z  ZS )SockSendfileMixinc                    s   t j| _dt _t   d S )Ni @  )r   !SENDFILE_FALLBACK_READBUFFER_SIZE_SockSendfileMixin__old_bufsizer$   r?   rD   r(   r
   r   r?      s    zSockSendfileMixin.setUpClassc                    s   | j t_t   d S r   )rS   r   rR   r$   rC   rD   r(   r
   r   rC      s    zSockSendfileMixin.tearDownClassTc                 C   s.   t  t jt j}|d |r*| |j |S r/   )socketAF_INETSOCK_STREAMsetblockingrG   r&   )r   cleanupsockr
   r
   r   make_socket   s
    
zSockSendfileMixin.make_socketc                 C   s   | tjtj| j d S r   )
setsockoptrT   
SOL_SOCKET	SO_RCVBUFrO   )r   rY   r
   r
   r   reduce_receive_buffer_size   s    z,SockSendfileMixin.reduce_receive_buffer_sizeNc                 C   s.   | tjtj| j |d ur*|j| jd d S )N)high)r[   rT   r\   	SO_SNDBUFrO   Zset_write_buffer_limits)r   rY   r   r
   r
   r   reduce_send_buffer_size   s    z)SockSendfileMixin.reduce_send_buffer_sizec                    s   t j t }jdd}|tj|f jj fdd|d	|  }j
|d|f |  fdd}| | fS )	NF)rX   c                      s    S r   r
   r
   )protor
   r   <lambda>       z8SockSendfileMixin.prepare_socksendfile.<locals>.<lambda>)rY   z	127.0.0.1c                      s<    j d ur" j          d S r   )r   r&   rN   r4   r
   rb   r   serverr
   r   rX      s
    

z7SockSendfileMixin.prepare_socksendfile.<locals>.cleanup)r.   r   r   find_unused_portrZ   bindHOSTrN   create_serverr^   Zsock_connectra   rG   )r   portsrv_sockrY   rX   r
   re   r   prepare_socksendfile   s    





z&SockSendfileMixin.prepare_socksendfilec                 C   sv   |   \}}| | j|| j}|  | |  | |t| j	 | |j
| j	 | | j t| j	 d S r   )rm   rN   r   sock_sendfilerF   r&   r4   assertEqualr"   r>   r   tellr   rY   rb   retr
   r
   r   test_sock_sendfile_success   s    z,SockSendfileMixin.test_sock_sendfile_successc                 C   sv   |   \}}| | j|| jdd}|  | |  | |j| j	dd  | | j
 d | |d d S )N  i  i  )rm   rN   r   rn   rF   r&   r4   ro   r   r>   rp   rq   r
   r
   r   (test_sock_sendfile_with_offset_and_count   s    z:SockSendfileMixin.test_sock_sendfile_with_offset_and_countc              	   C   s   |   \}}t (}| | j||dd }W d    n1 sB0    Y  |  | |  | |d | | j	
 d d S )Nr   )rm   tempfileTemporaryFilerN   r   rn   r&   r4   ro   rF   rp   )r   rY   rb   frr   r
   r
   r   test_sock_sendfile_zero_size   s    
$z.SockSendfileMixin.test_sock_sendfile_zero_sizec                 C   s   dd }|   \}}| | j|| | | j|| j}| | j|| |  | |  | |t	| j
 || j
 | }| |j| | | j t	| j
 d S )Ns   mix_regular_sendr7   )rm   rN   r   Zsock_sendallrn   rF   r&   r4   ro   r"   r>   r   rp   )r   bufrY   rb   rr   Zexpectedr
   r
   r   (test_sock_sendfile_mix_with_regular_send   s    z:SockSendfileMixin.test_sock_sendfile_mix_with_regular_send)T)N)r*   r+   r,   rP   r?   rC   rZ   r^   ra   rm   rs   ru   ry   r{   r-   r
   r
   r(   r   rQ      s   



rQ   c                   @   s   e Zd ZdddddZeejdk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dd Zeejd d!d"d# Zd$d% Zeeed& d'd(d) Zd*d+ Zd,S )-SendfileMixinFr   is_sslr   c                   s   t  }tj|d|r:ts(d t }t }nd }d }t		t	j
t	j}|t j|f jjfdd||d| |rt j}nd }t		t	j
t	j}|t j|f tjd jj fdd|||d\}	}
j||	d	  fd
d}|  fS )N)r   r   zNo ssl modulec                      s    S r   r
   r
   )	srv_protor
   r   rc     rd   z0SendfileMixin.prepare_sendfile.<locals>.<lambda>)rY   ssl)r   c                      s    S r   r
   r
   )	cli_protor
   r   rc     rd   )rY   r   server_hostname)r   c                      sF   j    j   j  j     d S r   )r   r&   rN   r   r4   r
   r   r   rf   r   r
   r   rX     s    

z/SendfileMixin.prepare_sendfile.<locals>.cleanup)r   rg   r   r   r   skipTestrI   Zsimple_server_sslcontextZsimple_client_sslcontextrT   rU   rV   rh   ri   rN   rj   r^   connectcreate_connectionra   rG   )r   r~   r   rk   Zsrv_ctxZcli_ctxrl   r   Zcli_socktrprrX   r
   r   r   prepare_sendfile   s>    




	
zSendfileMixin.prepare_sendfilewin32zUDP sockets are not supportedc                 C   s   |  | jjtjtjd\}}z`| td& |  | j	|| j
 W d    n1 sX0    Y  | d| j
  W |  n
|  0 d S )N)familynot supportedr   )rN   r   Zcreate_datagram_endpointr	   ZDatagramProtocolrT   rU   assertRaisesRegexRuntimeErrorsendfilerF   ro   rp   r&   )r   r   r   r
   r
   r   test_sendfile_not_supported"  s    "z)SendfileMixin.test_sendfile_not_supportedc                 C   s   |   \}}| | j|j| j}|j  | |j | |t	| j
 | |jt	| j
 | |j| j
 | | j t	| j
 d S r   r   rN   r   r   r   rF   r&   r   ro   r"   r>   r   r   rp   r   r   r   rr   r
   r
   r   test_sendfile1  s    
zSendfileMixin.test_sendfilec                    s      \}} fdd}| j_  j|j j}|j   |j  	|t
 j  	|jt
 j  	|j j  	 j t
 j d S )Nc                    s   t j j| |||S r   r   ZBaseEventLoop_sendfile_nativer   ZtransprF   offsetcountr   r
   r   sendfile_native?  s    zCSendfileMixin.test_sendfile_force_fallback.<locals>.sendfile_native)r   r   r   rN   r   r   rF   r&   r   ro   r"   r>   r   r   rp   )r   r   r   r   rr   r
   r   r   test_sendfile_force_fallback<  s    
z*SendfileMixin.test_sendfile_force_fallbackc                    s   t jdkr"t jtjr" d   \}} fdd}| j_ 	tj
d,   jj|j jdd W d    n1 s0    Y  |j   |j  |jd   j d d S )	Nr   zFails on proactor event loopc                    s   t j j| |||S r   r   r   r   r
   r   r   U  s    zMSendfileMixin.test_sendfile_force_unsupported_native.<locals>.sendfile_nativer   Ffallbackr   )sysplatform
isinstancer   r	   ProactorEventLoopr   r   r   r   ZSendfileNotAvailableErrorrN   r   r   rF   r&   r   ro   r   rp   )r   r   r   r   r
   r   r   &test_sendfile_force_unsupported_nativeO  s$    

"
z4SendfileMixin.test_sendfile_force_unsupported_nativec                 C   s   | j dd\}}| | j|j| j}|j  | |j | |t	| j
 | |jt	| j
 | |j| j
 | | j t	| j
 d S )NTr~   r   r   r
   r
   r   test_sendfile_sslg  s    
zSendfileMixin.test_sendfile_sslc                 C   s   |   \}}|j  | td( | | j|j| j W d    n1 sP0    Y  | |j	 | 
|jd | 
| j d d S )Nz
is closingr   )r   r   r&   r   r   rN   r   r   rF   r   ro   r   rp   r   r   r   r
   r
   r    test_sendfile_for_closing_transpr  s    
6z.SendfileMixin.test_sendfile_for_closing_transpc                 C   s   |   \}}dd }dd }|j| | | j|j| j}|j| |j  | |j | 	|t
| j | 	|j|| j |  | 	| j t
| j d S )Ns   PREFIX__   s   --SUFFIXr   r   r=   rN   r   r   rF   r&   r   ro   r"   r>   r   rp   r   r   r   ZPREFIXZSUFFIXrr   r
   r
   r   test_sendfile_pre_and_post_data{  s    
z-SendfileMixin.test_sendfile_pre_and_post_datac                 C   s   | j dd\}}dd }dd }|j| | | j|j| j}|j| |j  | |j | 	|t
| j | 	|j|| j |  | 	| j t
| j d S )NTr   s   zxcvbnmr   s
   0987654321r   r   r
   r
   r   #test_sendfile_ssl_pre_and_post_data  s    
z1SendfileMixin.test_sendfile_ssl_pre_and_post_datac                 C   s   |   \}}| | j|j| jdd}|j  | |j | |d | |j	d | |j
| jdd  | | j d d S )Nrt   d   L  r   rN   r   r   r   rF   r&   r   ro   r   r   r>   rp   r   r
   r
   r   test_sendfile_partial  s    
z#SendfileMixin.test_sendfile_partialc                 C   s   | j dd\}}| | j|j| jdd}|j  | |j | |d | |j	d | |j
| jdd  | | j d d S )NTr   rt   r   r   r   r   r
   r
   r   test_sendfile_ssl_partial  s    
z'SendfileMixin.test_sendfile_ssl_partialc                 C   s   | j t| jd\}}| | j|j| j}|j  | |j	 | 
|t| j | 
|jt| j | 
|j| j | 
| j t| j d S )Nr   )r   r"   r>   rN   r   r   r   rF   r&   r   ro   r   r   rp   r   r
   r
   r   (test_sendfile_close_peer_after_receiving  s    

z6SendfileMixin.test_sendfile_close_peer_after_receivingc                 C   s   | j dt| jd\}}| | j|j| j}| |j | 	|t| j | 	|j
t| j | 	|j| j | 	| j t| j d S )NTr}   )r   r"   r>   rN   r   r   r   rF   r   ro   r   r   rp   r   r
   r
   r   ,test_sendfile_ssl_close_peer_after_receiving  s    

z:SendfileMixin.test_sendfile_ssl_close_peer_after_receivingZsunoszDoesn't work on Solarisc                 C   s   | j dd\}}| t( | | j|j| j W d    n1 sH0    Y  | |j | 	d|j
  ko|t| jk n  |j
 | 	d| j   kot| jk n  | j  | 	|j  d S )Nr   r   )r   assertRaisesConnectionErrorrN   r   r   r   rF   r   
assertTruer   r"   r>   rp   
is_closingr   r
   r
   r   3test_sendfile_close_peer_in_the_middle_of_receiving  s    ""&zASendfileMixin.test_sendfile_close_peer_in_the_middle_of_receivingc                    s    fdd}| j _ jdd\}} t(   j |j j W d    n1 s\0    Y   |j	  
d|j  kot jk n  |j  
d j   kot jk n   j  d S )Nc                    s   t j j| |||S r   r   r   r   r
   r   r     s    zcSendfileMixin.test_sendfile_fallback_close_peer_in_the_middle_of_receiving.<locals>.sendfile_nativer   r   )r   r   r   r   r   rN   r   r   rF   r   r   r   r"   r>   rp   )r   r   r   r   r
   r   r   <test_sendfile_fallback_close_peer_in_the_middle_of_receiving  s    ""&zJSendfileMixin.test_sendfile_fallback_close_peer_in_the_middle_of_receivingr   z"Don't have native sendfile supportc                    s     \} j  fdd}j| } td  jd W d    n1 sl0    Y  |}	|t
j d S )Nc                      s"    d  j jjI d H S r   )r   r   r   r   rF   r
   r   r2   r   r
   r   rM     s    
z=SendfileMixin.test_sendfile_prevents_bare_write.<locals>.corozsendfile is in progresss   data)r   r   r   Zcreate_taskrN   r   r   r   r=   ro   r"   r>   )r   r   rM   trr   r
   r   r   !test_sendfile_prevents_bare_write  s    

*
z/SendfileMixin.test_sendfile_prevents_bare_writec                 C   sh   t  }dd |j_tjj|_| t	d* | j
| j
j|d dd W d    n1 sZ0    Y  d S )Nc                   S   s   dS r/   r
   r
   r
   r
   r   rc     rd   zPSendfileMixin.test_sendfile_no_fallback_for_fallback_transport.<locals>.<lambda>zfallback is disabledFr   )r   ZMockr   Zside_effectr   Z_SendfileModeZFALLBACKZ_sendfile_compatibler   r   r   rL   r   r   r
   r
   r   0test_sendfile_no_fallback_for_fallback_transport  s    
z>SendfileMixin.test_sendfile_no_fallback_for_fallback_transportN)r*   r+   r,   r   unittestZskipIfr   r   r   r   r   r   r   r   r   r   r   r   r   r   
startswithr   r   hasattrosr   r   r
   r
   r
   r   r|      s0   +
	

r|   c                   @   s   e Zd ZdS )SendfileTestsBaseN)r*   r+   r,   r
   r
   r
   r   r     s   r   r   c                   @   s   e Zd Zdd ZdS )SelectEventLoopTestsc                 C   s   t  S r   )r	   SelectorEventLoopr   r
   r
   r   r9     s    &SelectEventLoopTests.create_event_loopNr*   r+   r,   r9   r
   r
   r
   r   r     s   r   c                   @   s   e Zd Zdd ZdS )ProactorEventLoopTestsc                 C   s   t  S r   )r	   r   r   r
   r
   r   r9     s    z(ProactorEventLoopTests.create_event_loopNr   r
   r
   r
   r   r     s   r   KqueueSelectorc                   @   s   e Zd Zdd ZdS )KqueueEventLoopTestsc                 C   s   t t S r   )r	   r   	selectorsr   r   r
   r
   r   r9     s    z&KqueueEventLoopTests.create_event_loopNr   r
   r
   r
   r   r     s   r   EpollSelectorc                   @   s   e Zd Zdd ZdS )EPollEventLoopTestsc                 C   s   t t S r   )r	   r   r   r   r   r
   r
   r   r9   '  s    z%EPollEventLoopTests.create_event_loopNr   r
   r
   r
   r   r   $  s   r   PollSelectorc                   @   s   e Zd Zdd ZdS )PollEventLoopTestsc                 C   s   t t S r   )r	   r   r   r   r   r
   r
   r   r9   .  s    z$PollEventLoopTests.create_event_loopNr   r
   r
   r
   r   r   +  s   r   c                   @   s   e Zd Zdd ZdS )r   c                 C   s   t t S r   )r	   r   r   SelectSelectorr   r
   r
   r   r9   5  s    r   Nr   r
   r
   r
   r   r   2  s   __main__)%r	   r   rT   r   rv   r   r   r   r   testr   Ztest.supportr   Ztest.test_asyncior   rI   r   ImportErrorr   Protocolr   r.   r5   rQ   r|   r   r   ZTestCaser   r   r   r   r   r   r   r*   mainr
   r
   r
   r   <module>   sb   
&.m  
