
    1                        d dl m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	Z	d dl
mZmZmZmZmZmZmZ d dlmZmZmZ d dlmZ d dlmZ d d	lmZ d d
lmZmZ ej<                  rd dlm Z   G d d      Z!y)    )annotationsN)ResponseNotReady)mock)RECENT_DATECertificateErrorHTTPConnectionHTTPSConnection_match_hostname_url_from_connection_wrap_proxy_error)	HTTPError
ProxyErrorSSLError)ssl_)SKIP_HEADER)r   )_dnsname_matchmatch_hostname)_TYPE_PEER_CERT_RET_DICTc                     e 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+dZej$                  j'                  ddgg d      	 	 	 	 	 	 d,d       Zd+dZd+dZd+dZd+dZd+dZej$                  j'                  dddg      d-d       Zd+dZd+dZd+dZd+dZej$                  j'                  d g d!      ej$                  j'                  d"g d#      ej$                  j'                  d$g d%      ej$                  j'                  d&d'd(g      	 	 	 	 	 	 	 	 	 	 d.d)                            Zy*)/TestConnectionzR
    Tests in this suite should not make any network requests or connections.
    c                    d }d}t        j                  t              5  t        ||       d d d        y # 1 sw Y   y xY wNfoopytestraises
ValueErrorr
   selfcertasserted_hostnames      ;platform/gsutil/third_party/urllib3/test/test_connection.pytest_match_hostname_no_certz*TestConnection.test_match_hostname_no_cert%   s0    !]]:&D"34 '&&   4=c                    i }d}t        j                  t              5  t        ||       d d d        y # 1 sw Y   y xY wr   r   r   s      r"   test_match_hostname_empty_certz-TestConnection.test_match_hostname_empty_cert+   s0    )+!]]:&D"34 '&&r$   c                (    ddi}d}t        ||       y )NsubjectAltName)DNSr   r   r
   r   s      r"   test_match_hostname_matchz(TestConnection.test_match_hostname_match1   s    *:<M)N!/0    c                   ddi}d}	 t        j                  d      5 }t        ||       d d d        y # 1 sw Y   y xY w# t        $ r?}dt	        |      v sJ j                  ddddi       |j                  |k(  sJ Y d }~y d }~ww xY w)Nr(   r)   barurllib3.connection.log.warningz"hostname 'bar' doesn't match 'foo'@Certificate did not match expected hostname: %s. Certificate: %sr   patchr
   r   strassert_called_once_with
_peer_certr   r    r!   mock_loges        r"   test_match_hostname_mismatchz+TestConnection.test_match_hostname_mismatch6   s    *:<M)N!
	(<=&78 >== 	(73q6AAA,,R!#45
 <<4'''	()   ? 3? <? ? 	B5BBc                   ddi}d}	 t        j                  d      5 }t        ||       d d d        y # 1 sw Y   y xY w# t        $ r?}dt	        |      v sJ j                  ddddi       |j                  |k(  sJ Y d }~y d }~ww xY w)Nr(   ))r*    r/   r0   zhostname 'bar' doesn't match ''r1   r2   r7   s        r"   test_match_hostname_no_dnsz)TestConnection.test_match_hostname_no_dnsE   s    *:N)K!
	(<=&78 >== 	(4A>>>,,R!>2
 <<4'''	(r;   c                (    ddi}d}t        ||       y )Nr(   ))r*   *r   r+   r   s      r"   &test_match_hostname_startwith_wildcardz5TestConnection.test_match_hostname_startwith_wildcardT   s    *:O)L!/0r-   c                (    ddi}d}t        ||       y )Nr(   ))r*   xn--p1b6ci4b4b3a*.xn--11b5bs8drC   r+   r   s      r"   test_match_hostname_dnsnamez*TestConnection.test_match_hostname_dnsnameY   s"    J*
 =/0r-   c                (    ddi}d}t        ||       y )Nr(   )r*   zfoo*foobarr+   r   s      r"   $test_match_hostname_include_wildcardz3TestConnection.test_match_hostname_include_wildcard`   s    *:<N)O$/0r-   c                    ddi}d}t        j                  t        d      5  t        ||       d d d        y # 1 sw Y   y xY w)Nr(   )rF   )r*   zfo*r/   zdoesn't match either ofmatch)r   r   r   r
   r   s      r"   /test_match_hostname_more_than_one_dnsname_errorz>TestConnection.test_match_hostname_more_than_one_dnsname_errore   s?    ?*
 "]]+3LMD"34 NMM	   8Ac                |    t        j                  t        d      5  t        dd       d d d        y # 1 sw Y   y xY w)Nz!too many wildcards in certificaterJ   zfoo**rG   )r   r   r   r   )r   s    r"   7test_dnsname_match_include_more_than_one_wildcard_errorzFTestConnection.test_dnsname_match_include_more_than_one_wildcard_errorm   s)    ]]+3VW7H- XWWs   2;c                    ddi}d}t        j                  t        d      5  t        ||       d d d        y # 1 sw Y   y xY w)Nsubject))
commonNamer   r   z/no appropriate subjectAltName fields were foundrJ   )r   r   ImplementationCertificateErrorr   r   s      r"   &test_match_hostname_ignore_common_namez5TestConnection.test_match_hostname_ignore_common_nameq   sB    *35P)Q!]]*C
 4!23	
 
 
rM   c                *    ddi}d}t        ||d       y )NrQ   rR   r   Tr   r   s      r"   %test_match_hostname_check_common_namez4TestConnection.test_match_hostname_check_common_namez   s     *35P)Q!t.5r-   c                   ddi}d}	 t        j                  d      5 }t        ||       d d d        y # 1 sw Y   y xY w# t        $ r?}dt	        |      v sJ j                  ddddi       |j                  |k(  sJ Y d }~y d }~ww xY w)Nr(   ))
IP Addressz1.1.1.1z1.1.1.2r0   z*hostname '1.1.1.2' doesn't match '1.1.1.1'r1   r2   r7   s        r"   test_match_hostname_ip_addressz-TestConnection.test_match_hostname_ip_address   s    :*
 &
	(<=&78 >== 	(?3q6III,,R!#?@
 <<4'''	(r;   r!   san_ip))z1:2::3:41:2:0:0:0:0:3:4)z1:2:0:0::3:4r]   )z	::0.1.0.2z0:0:0:0:0:0:1:2)z::1%42z0:0:0:0:0:0:0:1)z	::2%ifacez0:0:0:0:0:0:0:2c                *    dd|ffi}t        ||       y)z;Check that hostname matches follow RFC 9110 rules for IPv6.r(   rZ   NrW   )r   r!   r\   r    s       r"   #test_match_hostname_ip_address_ipv6z2TestConnection.test_match_hostname_ip_address_ipv6   s!     +;lF=S<U)Vt./r-   c                   ddi}d}	 t        j                  d      5 }t        ||       d d d        y # 1 sw Y   y xY w# t        $ r?}dt	        |      v sJ j                  ddddi       |j                  |k(  sJ Y d }~y d }~ww xY w)Nr(   )rZ   z1:2::2:1z1:2::2:2r0   z,hostname '1:2::2:2' doesn't match '1:2::2:1'r1   r2   r7   s        r"   0test_match_hostname_ip_address_ipv6_doesnt_matchz?TestConnection.test_match_hostname_ip_address_ipv6_doesnt_match   s    ;*
 '
	(<=&78 >== 	(ASVKKK,,R!#@A
 <<4'''	(r;   c                    ddi}d}t        j                  t              5 }t        ||       d d d        dt	        j
                        v sJ y # 1 sw Y   #xY w)Nr(   ))r*   	localhost)rZ   rd   z[localhost]zGhostname '[localhost]' doesn't match either of 'localhost', 'localhost')r   r   r   r
   r4   value)r   r    r!   r9   s       r"   2test_match_hostname_dns_with_brackets_doesnt_matchzATestConnection.test_match_hostname_dns_with_brackets_doesnt_match   sa     *
 *]]+,D"34 - V177|	
 -,s   AAc                (    ddi}d}t        ||       y )Nr(   ra   z
[1:2::2:1]r+   r   s      r"   ,test_match_hostname_ip_address_ipv6_bracketsz;TestConnection.test_match_hostname_ip_address_ipv6_brackets   s"    ;*
 )/0r-   c                    t        j                  d      }t        t         j                   j                         |z
  j	                         kD  sJ y )Ni  )days)datetime	timedeltar   todaydate)r   	two_yearss     r"   test_recent_datezTestConnection.test_recent_date   s?    
 &&G4	h//557)CIIKKKKr-   c                    t        dd      }|j                  t        j                  t        j                  dfgk(  sJ y )Nznot.a.real.host  port   )r	   socket_optionssocketIPPROTO_TCPTCP_NODELAYr   conns     r"   +test_HTTPSConnection_default_socket_optionsz:TestConnection.test_HTTPSConnection_default_socket_options   s:    0s;""(:(:F<N<NPQ'R&SSSSr-   zproxy_scheme, err_part)httpzUnable to connect to proxy)httpszMUnable to connect to proxy. Your proxy appears to only use HTTP and not HTTPSc                |    t        t        d      |      }t        |t              du sJ ||j                  d   v sJ y )Nzunknown protocolTr   )r   r   
isinstancer   args)r   proxy_schemeerr_partnew_errs       r"   test_wrap_proxy_errorz$TestConnection.test_wrap_proxy_error   sA     $I.@$A<P':.$6667<<?***r-   c                J    t        dd      }d}d| t        ||      k(  sJ y )N
google.comP   rs   zpath?query=foozhttp://google.com:80/)r   r   )r   r{   paths      r"   test_url_from_poolz!TestConnection.test_url_from_pool   s0    l4&tf-1EdD1QQQQr-   c                    t        dd      }t        j                  t              5  |j	                          d d d        y # 1 sw Y   y xY w)Nr   r   rs   )r   r   r   r   getresponserz   s     r"   (test_getresponse_requires_reponseoptionsz7TestConnection.test_getresponse_requires_reponseoptions   s5    l4 ]]+, -,,s   AA
c                   t        j                  t        j                        }d|j                  j
                  j                  _        t        ddd|      }t         j                  j                  |d      5  t        j                  t              5  |j                          d d d        d d d        |j                  j
                  j                  j                          y # 1 sw Y   @xY w# 1 sw Y   DxY w)Ns	   fake certr   rr   z;AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA)rt   assert_fingerprintssl_context	_new_conn)r   create_autospecr   
SSLContextwrap_socketreturn_valuegetpeercertr	   r3   objectr   r   r   connectcloser5   r   contextr{   s      r"   %test_assert_fingerprint_closes_socketz4TestConnection.test_assert_fingerprint_closes_socket   s    &&t7DP((44A\	
 ZZt[1x( ) 2 	((..FFH )( 21s$   8C.C"#C."C+	'C..C7c                   t        j                  t        j                        }ddi|j                  j
                  j                  _        t        ddd|      }t         j                  j                  |d      5  t        j                  t              5  |j                          d d d        d d d        |j                  j
                  j                  j                          y # 1 sw Y   @xY w# 1 sw Y   DxY w)Nr(   ))r*   r   r   rr   zexample.com)rt   assert_hostnamer   r   )r   r   r   r   r   r   r   r	   r3   r   r   r   rT   r   r   r5   r   s      r"   "test_assert_hostname_closes_socketz1TestConnection.test_assert_hostname_closes_socket   s    &&t76E
((44A sMw
 ZZt[1=> ? 2 	((..FFH ?> 21s$   :C0C$%C0$C-	)C00C9accept_encoding)Accept-Encodingzaccept-encodings   Accept-Encodings   accept-encodingNhost)Hostr   s   Hosts   hostN
user_agent)
User-Agentz
user-agents
   User-Agents
   user-agentNchunkedTFc                   i }|	t         ||<   |	t         ||<   |	t         ||<   t        j                  d      5  t        j                  d      5 }t        d      }|j	                  dd||       d d d        d d d        i }j
                  D ]  }	|	j                  \  }
}|||
<    |d|v sJ ||vsJ |d|v sJ ||vsJ |d	|v sJ y ||vsJ y # 1 sw Y   dxY w# 1 sw Y   hxY w)
Nz)urllib3.util.connection.create_connectionz,urllib3.connection._HTTPConnection.putheaderr=   GETz/headers)headersr   r   r   r   )r   r   r3   r   requestcall_args_listr   )r   r   r   r   r   r   http_client_putheaderr{   request_headerscallheaderre   s               r"   test_skip_headerzTestConnection.test_skip_header  s,   , &'2GO$'GDM!"-GJ ZZCD>&%b)UJQ	 E )88D IIMFE&+OF# 9 "$777"/999<_,,,...?222_444-  EDs#   C!C0CC	CC(N)returnNone)r!   r4   r\   r4   r   r   )r   r4   r   r4   r   r   )
r   
str | Noner   r   r   r   r   boolr   r   ) __name__
__module____qualname____doc__r#   r&   r,   r:   r>   rA   rD   rH   rL   rO   rU   rX   r[   r   markparametrizer_   rb   rf   rh   rp   r|   r   r   r   r   r   r    r-   r"   r   r       s   551
((1
11
5.46
(" [[	h'	
	0!$0.10	0	0("
1LT [[ 2	
	+	+
RII [[	
	 [[V%MN[[V [[Yu6(5#(5 (5 	(5
 (5 
(5 7 O	(5r-   r   )"
__future__r   rk   rw   typinghttp.clientr   unittestr   r   urllib3.connectionr   r   r   r	   r
   r   r   urllib3.exceptionsr   r   r   urllib3.utilr   urllib3.util.requestr   urllib3.util.ssl_match_hostnamerT   r   r   TYPE_CHECKINGurllib3.util.ssl_r   r   r   r-   r"   <module>r      sZ    "    (     ? >  , K	:e5 e5r-   