a
    zeQ                     @   s  d Z 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rBG dd deejZG dd deejZn|ddlZe edrjG 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 )'z!Tests for sendfile functionality.    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 )NINITIALr   )		transportstatenbytescreate_future	connecteddone	bytearraydataclose_after)selfloopr   r
   r
   r   __init__   s    

zMySendfileProto.__init__c                 C   s6   || _ | jdksJ | jd| _| jr2| jd  d S )Nr   	CONNECTED)r   r   r   
set_resultr   r   r
   r
   r   connection_made&   s
    zMySendfileProto.connection_madec                 C   s   | j dksJ | j d| _ d S )Nr   EOF)r   r   r
   r
   r   eof_received-   s    zMySendfileProto.eof_receivedc                 C   s0   | j dv sJ | j d| _ | jr,| jd  d S )N)r   r   ZCLOSED)r   r   r   r   excr
   r
   r   connection_lost1   s    zMySendfileProto.connection_lostc                    s^   | j dksJ | j |  jt|7  _| j| t | | jrZ| j| jkrZ| j	  d S )Nr   )
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)r3   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 r6   )r4   r5   r   r"   r
   r
   r   r$   P   s    zMyProto.connection_lostc                    s   | j I d H  d S r   )r5   r    r
   r
   r   wait_closedT   s    zMyProto.wait_closedN)r-   r.   r/   r   r   r(   r$   r7   r
   r
   r
   r   r1   @   s
   r1   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   rB   g   s    *zSendfileBase.setUpClassc                    s   t t j t   d S r   )r   unlinkr?   r'   tearDownClassrC   r+   r
   r   rF   m   s    zSendfileBase.tearDownClassc                    s@   t tjd| _| | jj |  | _| | j t	 
  d S )Nrb)r>   r   r?   file
addCleanupr)   r<   r   Zset_event_loopr'   setUpr    r+   r
   r   rK   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   rN   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/   rA   BUF_SIZEr<   classmethodrB   rF   rK   rN   rQ   r0   r
   r
   r+   r   r8   X   s   	r8   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'   rB   rG   r+   r
   r   rB      s    zSockSendfileMixin.setUpClassc                    s   | j t_t   d S r   )rV   r   rU   r'   rF   rG   r+   r
   r   rF      s    zSockSendfileMixin.tearDownClassTc                 C   s.   t  t jt j}|d |r*| |j |S r2   )socketAF_INETSOCK_STREAMsetblockingrJ   r)   )r   cleanupsockr
   r
   r   make_socket   s
    
zSockSendfileMixin.make_socketc                 C   s   | tjtj| j d S r   )
setsockoptrW   
SOL_SOCKET	SO_RCVBUFrR   )r   r\   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^   rW   r_   	SO_SNDBUFrR   Zset_write_buffer_limits)r   r\   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)r[   c                      s    S r   r
   r
   )protor
   r   <lambda>       z8SockSendfileMixin.prepare_socksendfile.<locals>.<lambda>)r\   z	127.0.0.1c                      s<    j d ur" j          d S r   )r   r)   rQ   r7   r
   re   r   serverr
   r   r[      s
    

z7SockSendfileMixin.prepare_socksendfile.<locals>.cleanup)r1   r   r   find_unused_portr]   bindHOSTrQ   create_serverra   Zsock_connectrd   rJ   )r   portsrv_sockr\   r[   r
   rh   r   prepare_socksendfile   s    





z&SockSendfileMixin.prepare_socksendfilec                 C   sv   |   \}}| | j|| j}|  | |  | |t| j	 | |j
| j	 | | j t| j	 d S r   )rp   rQ   r   sock_sendfilerI   r)   r7   assertEqualr%   rA   r   tellr   r\   re   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  )rp   rQ   r   rq   rI   r)   r7   rr   r   rA   rs   rt   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   )rp   tempfileTemporaryFilerQ   r   rq   r)   r7   rr   rI   rs   )r   r\   re   fru   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_sendr:   )rp   rQ   r   Zsock_sendallrq   rI   r)   r7   rr   r%   rA   r   rs   )r   bufr\   re   ru   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/   rS   rB   rF   r]   ra   rd   rp   rv   rx   r|   r~   r0   r
   r
   r+   r   rT      s   



rT   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   rf     rg   z0SendfileMixin.prepare_sendfile.<locals>.<lambda>)r\   ssl)r   c                      s    S r   r
   r
   )	cli_protor
   r   rf     rg   )r\   r   server_hostname)r   c                      sF   j    j   j  j     d S r   )r   r)   rQ   r   r7   r
   r   r   ri   r   r
   r   r[     s    

z/SendfileMixin.prepare_sendfile.<locals>.cleanup)r   rj   r   r   r   skipTestrL   Zsimple_server_sslcontextZsimple_client_sslcontextrW   rX   rY   rk   rl   rQ   rm   ra   connectcreate_connectionrd   rJ   )r   r   r   rn   Zsrv_ctxZcli_ctxro   r   Zcli_socktrprr[   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   )rQ   r   Zcreate_datagram_endpointr	   ZDatagramProtocolrW   rX   assertRaisesRegexRuntimeErrorsendfilerI   rr   rs   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   rQ   r   r   r   rI   r)   r   rr   r%   rA   r   r   rs   r   r   r   ru   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transprI   offsetcountr    r
   r   sendfile_native?  s    zCSendfileMixin.test_sendfile_force_fallback.<locals>.sendfile_native)r   r   r   rQ   r   r   rI   r)   r   rr   r%   rA   r   r   rs   )r   r   r   r   ru   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SendfileNotAvailableErrorrQ   r   r   rI   r)   r   rr   r   rs   )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   rQ   r   r   rI   r   rr   r   rs   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@   rQ   r   r   rI   r)   r   rr   r%   rA   r   rs   r   r   r   ZPREFIXZSUFFIXru   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 )Nrw   d   L  r   rQ   r   r   r   rI   r)   r   rr   r   r   rA   rs   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   rw   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%   rA   rQ   r   r   r   rI   r)   r   rr   r   r   rs   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%   rA   rQ   r   r   r   rI   r   rr   r   r   rs   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ConnectionErrorrQ   r   r   r   rI   r   
assertTruer   r%   rA   rs   
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   rQ   r   r   rI   r   r   r   r%   rA   rs   )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   rI   r
   r   r5   r   r
   r   rP     s    
z=SendfileMixin.test_sendfile_prevents_bare_write.<locals>.corozsendfile is in progresss   data)r   r   r   Zcreate_taskrQ   r   r   r   r@   rr   r%   rA   )r   r   rP   tru   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 r2   r
   r
   r
   r
   r   rf     rg   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   rO   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   r<     s    &SelectEventLoopTests.create_event_loopNr-   r.   r/   r<   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   r<     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   r<     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   r<   '  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   r<   .  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   r<   5  s    r   Nr   r
   r
   r
   r   r   2  s   __main__)&__doc__r	   r   rW   r   ry   r   r   r   r   testr   Ztest.supportr   Ztest.test_asyncior   rL   r   ImportErrorr   Protocolr   r1   r8   rT   r   r   r   ZTestCaser   r   r   r   r   r   r   r-   mainr
   r
   r
   r   <module>   sd   
&.m  
