o
    IXîhD8  ã                   @   sL  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
Z
d dlZd dlZd dlZd dlZe d¡ dd	„ Zd
d„ Ze de
jv d¡ZeejjejjƒZG dd„ deƒZeee jdZeee j dZ!eee j dZ"G dd„ dej#ƒZ$G dd„ dej#ƒZ%G dd„ dej#ƒZ&G dd„ dej#ƒZ'e(dkr¤e )¡  dS dS )é    N)Úsupport)Ú	os_helper)Úsocket_helper)ÚResourceDenied)Úsanepathname2urlÚnetworkc                 O   sN   t dƒD ] }z
| |i |¤ŽW   S  |y$ } z|}W Y d }~qd }~ww |‚)Né   )Úrange)ÚfuncÚexcÚargsÚkwargsÚiÚeÚlast_exc© r   ú>/opt/python-3.10.19/usr/lib/python3.10/test/test_urllib2net.pyÚ_retry_thrice   s   €þr   c                    s   ‡ ‡fdd„}|S )Nc                     s   t ˆˆ g| ¢R i |¤ŽS ©N)r   )r   r   ©r   r
   r   r   Úwrapped   s   z(_wrap_with_retry_thrice.<locals>.wrappedr   )r
   r   r   r   r   r   Ú_wrap_with_retry_thrice   s   r   ZTRAVISz%bpo-35411: skip FTP test on Travis CIc                   @   s(   e Zd Z	 dd„ Zdd„ Zddd„ZdS )	ÚTransientResourcec                 K   s   || _ || _d S r   )r   Úattrs)Úselfr   r   r   r   r   Ú__init__2   s   
zTransientResource.__init__c                 C   s   | S r   r   ©r   r   r   r   Ú	__enter__6   ó   zTransientResource.__enter__Nc                 C   s^   	 |d ur+t | j|ƒr-| j ¡ D ]\}}t||ƒs d S t||ƒ|kr& d S qtdƒ‚d S d S )Nz%an optional resource is not available)Ú
issubclassr   r   ÚitemsÚhasattrÚgetattrr   )r   Ztype_ÚvalueÚ	tracebackÚattrZ
attr_valuer   r   r   Ú__exit__9   s   
ÿùzTransientResource.__exit__)NNN)Ú__name__Ú
__module__Ú__qualname__r   r   r&   r   r   r   r   r   -   s
    r   )Úerrnoc                   @   s   e Zd ZdS )Ú	AuthTestsN)r'   r(   r)   r   r   r   r   r+   N   s    r+   c                   @   s   e Zd Zdd„ ZdS )ÚCloseSocketTestc                 C   sr   |   tjj¡ tj}t |¡  t|ƒ}|j	}|  
|j¡ | ¡  |  |j¡ W d   ƒ d S 1 s2w   Y  d S r   )Ú
addCleanupÚurllibÚrequestÚ
urlcleanupr   ÚTEST_HTTP_URLr   Útransient_internetÚ_urlopen_with_retryÚfpÚassertFalseÚclosedÚcloseÚ
assertTrue)r   ÚurlÚresponseÚsockr   r   r   Ú
test_closew   s   "ûzCloseSocketTest.test_closeN)r'   r(   r)   r<   r   r   r   r   r,   u   s    r,   c                   @   sd   e Zd Zdd„ Zedd„ ƒZdd„ Zdd„ Zd	d
„ Zdd„ Z	e
 d¡dd„ ƒZddd„Zdd„ ZdS )ÚOtherNetworkTestsc                 C   s   d S r   )ÚloggingÚ	getLoggerÚ
addHandlerÚStreamHandler)r   r>   Úloggerr   r   r   ÚsetUp†   r   zOtherNetworkTests.setUpc                 C   s&   ddd t jjfg}|  ||  ¡ ¡ d S )Nzftp://www.pythontest.net/READMEz*ftp://www.pythontest.net/non-existent-file)r.   ÚerrorÚURLErrorÚ
_test_urlsÚ_extra_handlers)r   Úurlsr   r   r   Útest_ftp   s   ÿþzOtherNetworkTests.test_ftpc              	   C   sŠ   t j}t|dƒ}z+| d¡ | ¡  dttj |¡ƒ dd t	j
jfg}| j||  ¡ dd W t |¡ nt |¡ w |  tt	jjd¡ d S )NÚwz	hi there
zfile:zfile:///nonsensename/etc/passwdT)Úretryz./relative_path/to/file)r   ÚTESTFNÚopenÚwriter7   r   ÚosÚpathÚabspathr.   rD   rE   rF   rG   ÚremoveZassertRaisesÚ
ValueErrorr/   Úurlopen)r   rL   ÚfrH   r   r   r   Ú	test_file˜   s   

ÿþzOtherNetworkTests.test_filec                 C   s\   d}t  |¡ tj |¡}tj |¡}|  | ¡ d¡ W d   ƒ d S 1 s'w   Y  d S )Nz)http://www.pythontest.net/index.html#frag©r   r2   r.   r/   ÚRequestrT   ÚassertEqualÚgeturl)r   Zurlwith_fragÚreqÚresr   r   r   Útest_urlwithfragÅ   ó   
ÿ"ýz"OtherNetworkTests.test_urlwithfragc                 C   s\   d}t  |¡ tj |¡}tj |¡}|  | ¡ d¡ W d   ƒ d S 1 s'w   Y  d S )Nz*http://www.pythontest.net/redir/with_frag/z)http://www.pythontest.net/elsewhere/#fragrW   )r   Zredirect_url_with_fragr[   r\   r   r   r   Útest_redirect_url_withfragÍ   r^   z,OtherNetworkTests.test_redirect_url_withfragc                 C   sª   t j}t |¡C tj ¡ }tj |¡}|  | 	¡ ¡ | 
|¡ |  | 	¡ ¡ |  | d¡¡ | dd¡ | 
|¡ |  | d¡d¡ W d   ƒ d S 1 sNw   Y  d S )Nz
User-agentz
User-Agentz
Test-Agent)r   r1   r   r2   r.   r/   Úbuild_openerrX   r5   Zheader_itemsrM   r8   Z
has_headerZ
add_headerrY   Z
get_header)r   r9   Úopenerr/   r   r   r   Útest_custom_headersÕ   s   


"÷z%OtherNetworkTests.test_custom_headersz XXX: http://www.imdb.com is gonec              	   C   sª   d}t  |¡D ztj |¡}W d   ƒ n1 sw   Y  W n ty.   |  d¡ Y nw |  |¡ tj |¡}| ¡ }|  |¡ W d   ƒ d S 1 sNw   Y  d S )Nzhttp://www.imdb.comzOurlopen failed for site not sending                            Connection:close)	r   r2   r.   r/   rT   rS   Zfailr8   Úread)r   ZURLr\   r[   r   r   r   Útest_sites_no_connection_closeâ   s   ÿ€ÿ
"ôz0OtherNetworkTests.test_sites_no_connection_closeTc                 C   sâ  dd l }dd l}| d¡j}tjj|Ž j}|rt|tj	j
ƒ}|D ]Î}| j|d¾ t|tƒr4|\}}	}
nd  }	}
t |¡• z	|||	tjƒ}W n( tyo } z|
rdd|
||	t|ƒ|f }|  ||
|¡ n‚ W Y d }~n`d }~ww zDt8 t& t | ¡ }|dt|ƒ ƒ W d   ƒ n1 sw   Y  W d   ƒ n1 sŸw   Y  W d   ƒ n1 s®w   Y  W n tyÆ   td| tjd Y nw | ¡  W d   ƒ n1 sÕw   Y  | d¡ W d   ƒ n1 séw   Y  q d S )	Nr   Ztest_urllib2)r9   z5Didn't get expected error(s) %s for %s %s, got %s: %szread %d bytesz<timeout: %s>)Úfilegš™™™™™¹?) Útimer>   r?   Údebugr.   r/   r`   rM   r   rD   rE   ZsubTestÚ
isinstanceÚtupler   r2   r   ZINTERNET_TIMEOUTÚOSErrorÚtypeZassertIsInstanceÚtime_outÚsocket_peer_resetÚioerror_peer_resetrc   ÚlenÚTimeoutErrorÚprintÚsysÚstderrr7   Úsleep)r   rH   ÚhandlersrK   rf   r>   rg   rT   r9   r[   Zexpected_errrU   ÚerrÚmsgÚbufr   r   r   rF   ÷   s`   
ÿþ€üÿþü€ € €ÿ€ìå€ÿzOtherNetworkTests._test_urlsc                 C   s2   g }t j ¡ }|  |j¡ | d¡ | |¡ |S )Né   )r.   r/   ZCacheFTPHandlerr-   Úclear_cacheZ
setTimeoutÚappend)r   ru   Zcfhr   r   r   rG     s   


z!OtherNetworkTests._extra_handlersN)T)r'   r(   r)   rC   Úskip_ftp_test_on_travisrI   rV   r]   r_   rb   ÚunittestÚskiprd   rF   rG   r   r   r   r   r=   …   s    	
-

'r=   c                   @   sh   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ ZdZe	dd„ ƒZ
e	dd„ ƒZe	dd„ ƒZe	dd„ ƒZdS )ÚTimeoutTestc                 C   s   |   tjj¡ d S r   )r-   r.   r/   r0   r   r   r   r   rC   *  s   zTimeoutTest.setUpc                 C   sp   |   t ¡ ¡ tj}tj|d d t|ƒ}|  |j	¡ |   |j
jj ¡ ¡ W d   ƒ d S 1 s1w   Y  d S ©N©Útimeout)ÚassertIsNoneÚsocketÚgetdefaulttimeoutr   r1   r   r2   r3   r-   r7   r4   ÚrawÚ_sockÚ
gettimeout©r   r9   Úur   r   r   Útest_http_basic.  s   "ýzTimeoutTest.test_http_basicc              
   C   s”   |   t ¡ ¡ tj}t |¡1 t d¡ zt|ƒ}|  	|j
¡ W t d ¡ nt d ¡ w |  |jjj ¡ d¡ W d   ƒ d S 1 sCw   Y  d S ©Né<   )rƒ   r„   r…   r   r1   r   r2   Úsetdefaulttimeoutr3   r-   r7   rY   r4   r†   r‡   rˆ   r‰   r   r   r   Útest_http_default_timeout6  s   
"ùz%TimeoutTest.test_http_default_timeoutc              
   C   s–   |   t ¡ ¡ tj}t |¡2 t d¡ zt|d d}|  	|j
¡ W t d ¡ nt d ¡ w |   |jjj ¡ ¡ W d   ƒ d S 1 sDw   Y  d S ©Nr   r   )rƒ   r„   r…   r   r1   r   r2   rŽ   r3   r-   r7   r4   r†   r‡   rˆ   r‰   r   r   r   Útest_http_no_timeoutB  s   
"ùz TimeoutTest.test_http_no_timeoutc                 C   sd   t j}t |¡  t|dd}|  |j¡ |  |jj	j
 ¡ d¡ W d   ƒ d S 1 s+w   Y  d S )Néx   r   )r   r1   r   r2   r3   r-   r7   rY   r4   r†   r‡   rˆ   r‰   r   r   r   Útest_http_timeoutN  s   "ýzTimeoutTest.test_http_timeoutzftp://www.pythontest.net/c                 C   sp   |   t ¡ ¡ tj| jd d t| jƒ}|  |j¡ |   |j	j	j
j ¡ ¡ W d   ƒ d S 1 s1w   Y  d S r€   )rƒ   r„   r…   r   r2   ÚFTP_HOSTr3   r-   r7   r4   r†   r‡   rˆ   ©r   rŠ   r   r   r   Útest_ftp_basicW  s   
"ýzTimeoutTest.test_ftp_basicc              
   C   s”   |   t ¡ ¡ t | j¡3 t d¡ zt| jƒ}|  |j	¡ W t d ¡ nt d ¡ w |  
|jjjj ¡ d¡ W d   ƒ d S 1 sCw   Y  d S rŒ   )rƒ   r„   r…   r   r2   r”   rŽ   r3   r-   r7   rY   r4   r†   r‡   rˆ   r•   r   r   r   Útest_ftp_default_timeout_  s   

"ùz$TimeoutTest.test_ftp_default_timeoutc              
   C   s–   |   t ¡ ¡ t | j¡4 t d¡ zt| jd d}|  |j	¡ W t d ¡ nt d ¡ w |   |j
j
jj ¡ ¡ W d   ƒ d S 1 sDw   Y  d S r   )rƒ   r„   r…   r   r2   r”   rŽ   r3   r-   r7   r4   r†   r‡   rˆ   r•   r   r   r   Útest_ftp_no_timeoutk  s   
"ùzTimeoutTest.test_ftp_no_timeoutc                 C   sd   t  | j¡" t| jdd}|  |j¡ |  |jjjj	 
¡ d¡ W d   ƒ d S 1 s+w   Y  d S r   )r   r2   r”   r3   r-   r7   rY   r4   r†   r‡   rˆ   r•   r   r   r   Útest_ftp_timeoutw  s
   "ýzTimeoutTest.test_ftp_timeoutN)r'   r(   r)   rC   r‹   r   r‘   r“   r”   r|   r–   r—   r˜   r™   r   r   r   r   r   )  s    


r   Ú__main__)*r*   r}   Útestr   Ztest.supportr   r   r   Ztest.test_urllib2r   rO   r„   Zurllib.errorr.   Zurllib.requestrr   Zrequiresr   r   ZskipIfÚenvironr|   r/   rT   rD   rE   r3   Úobjectr   rj   Z	ETIMEDOUTrl   Z
ECONNRESETrm   rn   ZTestCaser+   r,   r=   r   r'   Úmainr   r   r   r   Ú<module>   sB    
	ÿÿ' %Vÿ