Your IP : 216.73.216.196


Current Path : /opt/alt/python310/lib64/python3.10/test/__pycache__/
Upload File :
Current File : //opt/alt/python310/lib64/python3.10/test/__pycache__/test_urllib2_localnet.cpython-310.opt-2.pyc

o

0�i�e�@s�ddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlmZddlm
Z
ddlmZzddlZWneyGdZYnwej�e�Zej�ed�Zej�ed�ZGdd�dejj�ZGd	d
�d
ej�ZGdd�d�ZGd
d�dejj�ZGdd�dejj�Z Gdd�de	j!�Z"ej#ddd�Gdd�de	j!��Z$dd�Z%Gdd�de	j!�Z&dd�Z'e(dkr�e	�)�dSdS)�N)�hashlib_helper)�threading_helper)�warnings_helperzkeycert.pemzkeycert2.pemc@seZdZ	dd�Zdd�ZdS)�LoopbackHttpServercCs"tjj�|||�|j�d�dS)Ng�������?)�http�server�
HTTPServer�__init__�socket�
settimeout)�selfZserver_addressZRequestHandlerClass�r
�A/opt/alt/python310/lib64/python3.10/test/test_urllib2_localnet.pyr	"s

�zLoopbackHttpServer.__init__cCs"	|j��\}}|�d�||fS)Ng$@)r
Zacceptr)r�requestZclient_addressr
r
r�get_request+s
zLoopbackHttpServer.get_requestN)�__name__�
__module__�__qualname__r	rr
r
r
rrs	rc@�&eZdZ	dd�Zdd�Zdd�ZdS)�LoopbackHttpServerThreadcCs<tj�|�d|_t��|_d|_td|�|_|jj	|_
dS)NFzHTTP/1.0)z	127.0.0.1r)�	threading�Threadr	�_stop_serverZEvent�readyZprotocol_versionr�httpdZserver_port�port�r�request_handlerr
r
rr	:s
�z!LoopbackHttpServerThread.__init__cCs	d|_|��|j��dS)NT)r�joinrZserver_close�rr
r
r�stopCszLoopbackHttpServerThread.stopcCs(|j��|js|j��|jrdSdS�N)r�setrr�handle_requestrr
r
r�runKs

�zLoopbackHttpServerThread.runN)rrrr	r r$r
r
r
rr7s
	rc@sVeZdZ	dd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dd�ZdS)�DigestAuthHandlercCs"d|_g|_i|_d|_d|_dS)Nrz
Test Realm�auth)�_request_num�_nonces�_users�_realm_name�_qoprr
r
rr	Us

zDigestAuthHandler.__init__cC�
||_dSr!)r+)rZqopr
r
r�set_qop\�
zDigestAuthHandler.set_qopcCr,r!)r))rZusersr
r
r�	set_users_s
zDigestAuthHandler.set_userscCr,r!)r*)rZrealmr
r
r�	set_realmcr.zDigestAuthHandler.set_realmcCs8|jd7_t�t|j��d����}|j�|�|S)N��ascii)r'�hashlib�md5�str�encode�	hexdigestr(�append)r�noncer
r
r�_generate_noncefsz!DigestAuthHandler._generate_noncecCs�|�d�}||dd�}|�d�}i}|D](}|�d�\}}|��}|ddkr6|ddkr6|dd�}n|��}|||<q|S)N� r1�,�=r�"���)�find�split�strip)rZauth_strZfirst_space_index�parts�	auth_dict�part�name�valuer
r
r�_create_auth_dictls


z#DigestAuthHandler._create_auth_dictcCs�i}|�|�||d<||d<||d<d|}t�|�d����}d|}t�|�d����}	||d<|	|d<d	|}
t�|
�d����}||d
kS)N�password�method�uriz#%(username)s:%(realm)s:%(password)sr2z%(method)s:%(uri)s�HA1�HA2z3%(HA1)s:%(nonce)s:%(nc)s:%(cnonce)s:%(qop)s:%(HA2)s�response)�updater3r4r6r7)rrDrIrJrKZ
final_dictZHA1_strrLZHA2_strrMZresponse_strrNr
r
r�_validate_auth}s 
�z DigestAuthHandler._validate_authcCsN|�dd�|�dd�|�dd|j|j|��f�|��|j�d�dS)	Ni�zProxy Authentication Required�Content-Type�	text/htmlzProxy-Authenticatez(Digest realm="%s", qop="%s",nonce="%s", sProxy Authentication Required.F)�
send_response�send_headerr*r+r:�end_headers�wfile�writerr
r
r�_return_auth_challenge�s��z(DigestAuthHandler._return_auth_challengecCs�	t|j�dkr
dSd|jvr|�|�S|�|jd�}|d|jvr+|j|d}n|�|�S|�d�|jvr=|�|�S|j�|d�d}|j|j	fD]
}|�
|||j|�rZd}qM|sb|�|�SdS)NrTzProxy-AuthorizationZusernamer9F)�lenr)�headersrXrH�getr(�remove�path�
short_pathrPZcommand)rrrDrIZauth_validatedr]r
r
rr#�s4

�

��
z DigestAuthHandler.handle_requestN)rrrr	r-r/r0r:rHrPrXr#r
r
r
rr%Rsr%c@sdeZdZ	dZdZdZdeefZe�e�	d���
d�Zdd�Zdd	�Z
d
d�Zdd
�Zdd�ZdS)�BasicAuthHandler�testUser�testPass�Testz%s:%sr2cOs tjjj|g|�Ri|��dSr!)rr�BaseHTTPRequestHandlerr	)r�args�kwargsr
r
rr	�s zBasicAuthHandler.__init__cG�dSr!r
�r�formatrdr
r
r�log_message�szBasicAuthHandler.log_messagecCs"|�d�|�dd�|��dS)N���Content-typerR)rSrTrUrr
r
r�do_HEAD�s
zBasicAuthHandler.do_HEADcCs4|�d�|�dd|j�|�dd�|��dS)Ni�zWWW-AuthenticatezBasic realm="%s"rkrR)rSrT�REALMrUrr
r
r�do_AUTHHEAD�s
zBasicAuthHandler.do_AUTHHEADcCsl|j�dd�s|��|j�d�dS|j�dd�d|jkr0|�d�|��|j�d�dS|��dS)NZ
Authorization�sNo Auth header receivedzBasic rjsIt works)rZr[rnrVrW�ENCODED_AUTHrSrUrr
r
r�do_GET�s��
zBasicAuthHandler.do_GETN)rrr�USER�PASSWDrmZUSER_PASSWD�base64Z	b64encoder6�decoderpr	rirlrnrqr
r
r
rr_�sr_c@r)�FakeProxyHandlercOs&||_tjjj|g|�Ri|��dSr!)�digest_auth_handlerrrrcr	)rrwrdrer
r
rr	�s zFakeProxyHandler.__init__cGrfr!r
rgr
r
rriszFakeProxyHandler.log_messagecCsztj�|jd�\}}}}}}||_|j�|�r;|�dd�|�dd�|�	�|j
�td|jd��|j
�d�dSdS)	NrrjZOKrQrRzYou've reached %s!<BR>r2sFOur apologies, but our server is down due to a sudden zombie invasion.)
�urllib�parseZurlparser]r^rwr#rSrTrUrVrW�bytes)rZscmZnetlocr]�paramsZqueryZfragmentr
r
rrq	s���zFakeProxyHandler.do_GETN)rrrr	rirqr
r
r
rrv�s
rvcsPeZdZdZdZdZdZ�fdd�Zdd�Z�fd	d
�Z	dd�Z
d
d�Z�ZS)�BasicAuthTestsr`raZ	IncorrectrbcsTtt|���dd�}t|�|_|�|j�d|jj|_|j�	�|jj
��dS)Nc_st|i|��Sr!)r_�rdrer
r
r�#http_server_with_basic_auth_handler!szABasicAuthTests.setUp.<locals>.http_server_with_basic_auth_handlerzhttp://127.0.0.1:%s)�superr|�setUprr�
addCleanup�stop_serverr�
server_url�startr�wait)rr~��	__class__r
rr�s

zBasicAuthTests.setUpcC�|j��d|_dSr!�rr rr
r
rr�)�

zBasicAuthTests.stop_servercstt|���dSr!)rr|�tearDownrr�r
rr�-szBasicAuthTests.tearDowncCsztj��}|�|j|j|j|j�tj�tj�	|��z
|�
tj�|j��WdStjj
y<|�d|j�YdSw)Nz!Basic auth failed for the url: %s)rxr�HTTPBasicAuthHandler�add_passwordrmr�rrrs�install_opener�build_opener�
assertTrue�urlopen�error�	HTTPError�fail�rZahr
r
r�test_basic_auth_success0s
�z&BasicAuthTests.test_basic_auth_successcCsRtj��}|�|j|j|j|j�tj�tj�	|��|�
tjjtjj
|j�dSr!)rxrr�r�rmr�rr�INCORRECT_PASSWDr�r��assertRaisesr�r�r�r�r
r
r�test_basic_auth_httperror9s
z(BasicAuthTests.test_basic_auth_httperror)
rrrrrrsr�rmr�r�r�r�r��
__classcell__r
r
r�rr|s	r|r4T)ZopensslcsTeZdZdZdZdZdZ�fdd�Zdd�Zd	d
�Z	dd�Z
d
d�Zdd�Z�Z
S)�ProxyAuthTestszhttp://localhostZtesterZtest123Z	TestRealmcs�tt����dd�}��|tj���dtjd<dtjd<t��_�j�	�j
�ji��j��j
��fdd�}t|��_���j��j���jj��d�jj}tj�d	|i�}tj���_tj�|�j��_dS)
NcS�tj��tj�|�dSr!��os�environ�clearrO�Zold_environr
r
r�restore_environK�
z-ProxyAuthTests.setUp.<locals>.restore_environro�NO_PROXY�no_proxycst�jg|�Ri|��Sr!)rvrwr}rr
r�create_fake_proxy_handlerVsz7ProxyAuthTests.setUp.<locals>.create_fake_proxy_handlerzhttp://127.0.0.1:%dr)rr�r�r�r�r��copyr%rwr/rrrsr0rmrrr�r�rr�rrxrZProxyHandlerZProxyDigestAuthHandler�proxy_digest_handlerr��opener)rr�r�Z	proxy_url�handlerr�rrr�Hs&




�zProxyAuthTests.setUpcCr�r!r�rr
r
rr�cr�zProxyAuthTests.stop_servercCsF|j�|j|j|j|jd�|j�d�|�t	j
j|jj
|j�dS)NZbadr&)r�r�rm�URLrrrsrwr-r�rxr�r�r��openrr
r
r�-test_proxy_with_bad_password_raises_httperrorgs�
�z<ProxyAuthTests.test_proxy_with_bad_password_raises_httperrorcCs(|j�d�|�tjj|jj|j�dS�Nr&)	rwr-r�rxr�r�r�r�r�rr
r
r�,test_proxy_with_no_password_raises_httperroros

�z;ProxyAuthTests.test_proxy_with_no_password_raises_httperrorcCs||j�|j|j|j|j�|j�d�|j�	|j��}|�
�r,	|�
�sWd�dSWd�dS1s7wYdSr�)r�r�rmr�rrrsrwr-r�r��read�r�resultr
r
r�test_proxy_qop_auth_worksus�
��"�z(ProxyAuthTests.test_proxy_qop_auth_workscCs�|j�|j|j|j|j�|j�d�z	|j�	|j�}Wnt
jjy(YdSw|�|�
�r=	|�
�s0Wd�dSWd�dS1sHwYdS)Nzauth-int)r�r�rmr�rrrsrwr-r�r�rxr��URLErrorr�r�r
r
r�0test_proxy_qop_auth_int_works_or_throws_urlerror}s ��
��"�z?ProxyAuthTests.test_proxy_qop_auth_int_works_or_throws_urlerror)rrrr�rrrsrmr�r�r�r�r�r�r�r
r
r�rr�@sr�csG��fdd�dtjj���S)NcsBeZdZdZgZgZdZdd�Zdd�Z��fdd�Z	d	d
�Z
dS)z1GetRequestHandler.<locals>.FakeHTTPRequestHandlerz	TestHTTP/�PcSs0|��}|r|j�|�}||d�}|sdSdSr!)�	send_headrVrW)r�bodyZdoner
r
rrq�s
�z8GetRequestHandler.<locals>.FakeHTTPRequestHandler.do_GETcSs2|jd}|j�t|��}|��|j�|�dS)NzContent-Length)rZZrfiler��intrq�requestsr8)rZcontent_lengthZ	post_datar
r
r�do_POST�s
z9GetRequestHandler.<locals>.FakeHTTPRequestHandler.do_POSTcs||j�_|j�|j���d�\}}}|�|�|D]\}}|�||d|ji�q|r8|�dd�|�	�|S|�	�dS)Nrrrkz
text/plain)
rZ�headers_receivedr�r8r]�poprSrTrrU)rZ
response_coderZr��headerrG��FakeHTTPRequestHandler�	responsesr
rr��s
z;GetRequestHandler.<locals>.FakeHTTPRequestHandler.send_headcWrfr!r
)rrdr
r
rri�sz=GetRequestHandler.<locals>.FakeHTTPRequestHandler.log_messageN)rrrZserver_versionr�r�rrqr�r�rir
r�r
rr��sr�)rrrc)r�r
r�r�GetRequestHandler�s&r�cs�eZdZ	�fdd�Zd.dd�Zdd�Zd.dd	�Zd.d
d�Zdd
�Zdd�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zd d!�Zd"d#�Zd$d%�Zd&d'�Zd(d)�Zd*d+�Zd,d-�Z�ZS)/�TestUrlopencsNtt|���|�tjj�dd�}|�|tj�	��dtjd<dtjd<dS)NcSr�r!r�r�r
r
rr��r�z*TestUrlopen.setUp.<locals>.restore_environ�*r�r�)
rr�r�r�rxrZ
urlcleanupr�r�r�)rr�r�r
rr��s
zTestUrlopen.setUpNcKsxg}tjj||fi|��}z$|�|�d��|�|���|�|�d��|�|���W|��n|��wd�	|�S)Nrji�)
rxrr��extend�	readlinesr8�readliner��closer)r�url�datare�l�fr
r
rr��s
zTestUrlopen.urlopencCr�r!r�rr
r
rr��r�zTestUrlopen.stop_servercCsZ|dur
dgdfg}t|�}t|�|_|�|j�|j��|jj��|jj}||_|S)Nrjs
we don't care)	r�rrr�r�r�rr�r)rr�r�rr
r
r�start_server�s

zTestUrlopen.start_servercKs^ttjd�s|�d�ddlm}|durdgdfg}t|�}||fd|i|��}|j|_|S)NZHTTPSHandlerzssl support requiredr)�make_https_serverrj�
we care a bitZ
handler_class)�hasattrrxr�skipTestZtest.ssl_serversr�r�r)rr�rer�r�rr
r
r�start_https_server�s
zTestUrlopen.start_https_servercCsVd}ddgdfdg|fg}|�|�}|�d|j�}|�||�|�|jddg�dS)	NsWe got here...i.)ZLocationz(http://localhost:%(port)s/somewhere_elserorj�http://localhost:%s/�/z/somewhere_else�r�r�r�assertEqualr�)r�expected_responser�r�r�r
r
r�test_redirection�s��
zTestUrlopen.test_redirectioncCs@d}d}ddg|fg}|�|�}|�d|j�}|�||�dS)Nshello worldsa
hello worl
1
d
0
rj)zTransfer-EncodingZchunkedr�)r�r�rr�)rr�Z
chunked_startrNr�r�r
r
r�test_chunkeds�
zTestUrlopen.test_chunkedc
Cs�d}|�dg|fg�}z
|�d|j�Wntjjy1}z|��}|��WYd}~n
d}~ww|�d�|�	||�|�	|j
dg�dS)NsBad bad bad...i�zhttp://localhost:%s/weeblez404 should raise URLErrorz/weeble)r�r�rrxr�r�r�r�r�r�r�)rr�r�r�r�r
r
r�test_404s��
zTestUrlopen.test_404cCsFd}|�dg|fg�}|�d|j�}|�||�|�|jdg�dS)N�
pycon 2008...rj�http://localhost:%s/bizarre�/bizarrer��rr�r�r�r
r
r�test_200#s
zTestUrlopen.test_200cCsJd}|�dg|fg�}|�d|jd�}|�||�|�|jddg�dS)Nr�rjr�sget=with_feelingr�r�r�r
r
r�test_200_with_parameters*s�z$TestUrlopen.test_200_with_parameterscCs8|��}tjtd�}|jd|j|d�}|�|d�dS)N�Zcafile�https://localhost:%s/bizarre��contextr�)r��ssl�create_default_context�CERT_localhostr�rr�)rr�r�r�r
r
r�
test_https2szTestUrlopen.test_httpsc	Cs|jtd�}t�dtf��i|jd|jtd�}|�|d�|�t	j
j��}|jd|jtd�Wd�n1s:wY|jtd�}|�t	j
j��}|jd|jtd�Wd�n1sawYWd�dSWd�dS1sywYdS)N��certfileror�r�r�)
r�r�r�check_warnings�DeprecationWarningr�rr�r�rxr�r��CERT_fakehostname)rr�r��cmr
r
r�test_https_with_cafile8s(������"�z"TestUrlopen.test_https_with_cafilec	Cs�|jtd�}t�dtf��2|�tjj��}|j	d|j
dd�Wd�n1s*wYWd�dSWd�dS1sBwYdS)Nr�ror�T)Z	cadefault)r�r�rr�r�r�rxr�r�r�r)rr�r�r
r
r�test_https_with_cadefaultIs���"�z%TestUrlopen.test_https_with_cadefaultcs�tdur	|�d�tjs|�d�d��fdd�}t�tj�}|�|�|j|td�}tjtd�}|j	d|j
|d�|��d	�dS)
Nzssl module requiredzSNI support required in OpenSSLcs|�dSr!r
)Zssl_sockZserver_nameZinitial_context�Zsni_namer
r�cb_sniWsz*TestUrlopen.test_https_sni.<locals>.cb_sni)r�r�r�zhttps://localhost:%sr�Z	localhost)r�r�ZHAS_SNIZ
SSLContextZPROTOCOL_TLS_SERVERZset_servername_callbackr�r�r�r�rr�)rr�r�r�r
r�r�test_https_sniQs


zTestUrlopen.test_https_snicCsd|��}tjjd|jddid�}tj�|��Wd�n1s"wY|�|jdd�dS)Nr�ZRangezbytes=20-39�rZ)r�rxr�Requestrr�r�r��rr�Zreqr
r
r�test_sending_headersas��z TestUrlopen.test_sending_headerscCsv|��}tjjd|jddid�}tj�|��Wd�n1s"wY|�d|j���|�	d|j���dS)Nr�z
X-SoMe-hEaderZfoobarr�z
X-Some-Header)
r�rxrr�rr�ZassertInr��keysZassertNotInr�r
r
r�test_sending_headers_camelis��z&TestUrlopen.test_sending_headers_camelcCsp|��}tj�d|j��!}dD]
}|�t||�d|�q|�|��d�Wd�dS1s1wYdS)N�http://localhost:%s)r�r��info�geturlz3object returned from urlopen lacks the %s attributezcalling 'read' failed)r�rxrr�rr�r�r�)rr��open_url�attrr
r
r�
test_basicrs�"�zTestUrlopen.test_basiccCsl|��}tj�d|j�}|�|��}Wd�n1swY|�|tjj	d�|�
|��d�dS)Nr�zEobject returned by 'info' is not an instance of email.message.MessageZplain)r�rxrr�rr�ZassertIsInstance�email�messageZMessager�Zget_content_subtype)rr�r�Zinfo_objr
r
r�	test_infozs�
��zTestUrlopen.test_infocCs\|��}tj�d|j�}|�|��}Wd�n1swY|�|d|j�dS)Nr�)r�rxrr�rr�r�)rr�r�r�r
r
r�test_geturl�s
�zTestUrlopen.test_geturlcCsBd}|�dg|fg�}tj�d|j�}|D]}|�||�qdS)Nr�rjr�)r�rxrr�rr�)rr�r�r��liner
r
r�test_iteration�s�zTestUrlopen.test_iterationc
Cs�ddddddg}d�|�}|�dg|fg�}tj�d	|j�}t|�D]\}}|�|||d
|t||�t|�f�q%|�|dt|��dS)NsWe
sgot
shere
s	verylong i �
r�rjr�zQFetched line number %s doesn't match expected:
    Expected length was %s, got %sr1)	rr�rxrr�r�	enumerater�rY)r�linesr�r�r��indexrr
r
r�test_line_iteration�s
��zTestUrlopen.test_line_iterationcCs�|�dgdfdgdfg�}tj��}tj�d|j�}|�d|j�|�|d�	d��|�d|j�|�d|�
d��|�|d�	d��|�d	|j�|�d
|�
d��dS)Nrjsanyr��1zus-ascii�1zContent-lengthZ
1234567890s
1234567890Z10)r�rxrr�r�rr�r�r�r6Z
get_header)rr�r�rr
r
r�test_issue16464�s�
zTestUrlopen.test_issue16464r!)rrrr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrrrrrr�r
r
r�rr��s0




	r�cCs t��}tjtjg|�R�dSr!)rZthreading_setup�unittestZaddModuleCleanupZthreading_cleanup)�thread_infor
r
r�setUpModule�sr�__main__)*rtr�rZurllib.parserxZurllib.requestZhttp.serverrrrr3Ztest.supportrrrr��ImportErrorr]�dirname�__file__�hererr�r�rrrrrr%rcr_rvZTestCaser|Zrequires_hashdigestr�r�r�rr�mainr
r
r
r�<module>sF�y,!(M+{�