o
    )ªZhÏA  ã                   @   s  d dl m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	m
Z
 d dlmZ d dlmZmZ d dlmZ d dlmZmZ d d	lmZmZmZ d d
lmZ d dlZejrgd dlmZ d dlmZm Z m!Z! d\Z"Z#G dd„ deƒZ$G dd„ deƒZ%G dd„ dej&ƒZ'G dd„ deƒZ(dS )é    )ÚclosingN©ÚFuture)Úbind_socketsÚResolver)ÚQueue)Ú	TCPClientÚ
_Connector)Ú	TCPServer)ÚAsyncTestCaseÚgen_test)ÚskipIfNoIPv6Úrefusing_portÚskipIfNonUnix)ÚTimeoutError)ÚIOStream)ÚListÚDictÚTuple)é   é   c                       s0   e Zd Z‡ fdd„Zdd„ Z‡ fdd„Z‡  ZS )ÚTestTCPServerc                    sD   t ƒ  ¡  g | _tƒ | _tdd|ƒ}|  |¡ |d  ¡ d | _d S )Nr   Ú	localhostr   )	ÚsuperÚ__init__Ústreamsr   Úqueuer   Zadd_socketsÚgetsocknameÚport)ÚselfÚfamilyZsockets©Ú	__class__© úR/var/www/html/lang_env/lib/python3.10/site-packages/tornado/test/tcpclient_test.pyr   *   s   

zTestTCPServer.__init__c                 C   s   | j  |¡ | j |¡ d S ©N)r   Úappendr   Úput)r   ÚstreamÚaddressr#   r#   r$   Úhandle_stream2   s   zTestTCPServer.handle_streamc                    s"   t ƒ  ¡  | jD ]}| ¡  qd S r%   )r   Ústopr   Úclose©r   r(   r!   r#   r$   r+   6   s   


ÿzTestTCPServer.stop)Ú__name__Ú
__module__Ú__qualname__r   r*   r+   Ú__classcell__r#   r#   r!   r$   r   )   s    r   c                       sÆ   e Zd Z‡ fdd„Zdd„ Zdd„ Z‡ fdd„Zd	d
„ Zed&dd„ƒZ	dd„ Z
dd„ Zedd„ ƒZedd„ ƒZdd„ Zedd„ ƒZdd„ Zedd„ ƒZdd„ Zd d!„ Zed"d#„ ƒZed$d%„ ƒZ‡  ZS )'ÚTCPClientTestc                    s   t ƒ  ¡  d | _tƒ | _d S r%   )r   ÚsetUpÚserverr   Úclient©r   r!   r#   r$   r3   =   s   
zTCPClientTest.setUpc                 C   s0   |t jkrdtjv r|  d¡ t|ƒ| _| jjS )NZTRAVISz6dual-stack servers often have port conflicts on travis)ÚsocketÚ	AF_UNSPECÚosÚenvironÚskipTestr   r4   r   )r   r    r#   r#   r$   Ústart_serverB   s   

zTCPClientTest.start_serverc                 C   s"   | j d ur| j  ¡  d | _ d S d S r%   )r4   r+   r6   r#   r#   r$   Ústop_serverH   s   


þzTCPClientTest.stop_serverc                    s    | j  ¡  |  ¡  tƒ  ¡  d S r%   )r5   r,   r=   r   ÚtearDownr6   r!   r#   r$   r>   M   s   
zTCPClientTest.tearDownc                 C   s>   | j  dd„ ¡}tdd„ |D ƒƒ}tj|vr|  d¡ d S d S )Nc                   S   s   t ƒ  dd¡S )Nr   éP   )r   Úresolver#   r#   r#   r$   Ú<lambda>U   s    z1TCPClientTest.skipIfLocalhostV4.<locals>.<lambda>c                 s   s    | ]}|d  V  qdS )r   Nr#   )Ú.0Úaddrr#   r#   r$   Ú	<genexpr>V   s   € z2TCPClientTest.skipIfLocalhostV4.<locals>.<genexpr>z"localhost does not resolve to ipv6)Úio_loopZrun_syncÚsetr7   ÚAF_INET6r;   )r   ÚaddrinfoZfamiliesr#   r#   r$   ÚskipIfLocalhostV4R   s
   
ÿzTCPClientTest.skipIfLocalhostV4Nc           	      c   s    |   |¡}| jj|||||dV }| jd usJ ‚| jj ¡ V }t|ƒ | d¡ | d¡V }|  	|d¡ W d   ƒ d S 1 sAw   Y  d S )N)Ú	source_ipÚsource_portÚafs   helloé   )
r<   r5   Úconnectr4   r   Úgetr   ÚwriteÚ
read_bytesÚassertEqual)	r   r    ÚhostrJ   rK   r   r(   Zserver_streamÚdatar#   r#   r$   Údo_test_connectZ   s    €
û

"ýzTCPClientTest.do_test_connectc                 C   ó   |   tjd¡ d S ©Nú	127.0.0.1©rU   r7   ÚAF_INETr6   r#   r#   r$   Útest_connect_ipv4_ipv4k   ó   z$TCPClientTest.test_connect_ipv4_ipv4c                 C   rV   ©Nr   rY   r6   r#   r#   r$   Útest_connect_ipv4_dualn   r\   z$TCPClientTest.test_connect_ipv4_dualc                 C   ó   |   ¡  |  tjd¡ d S ©Nz::1)rI   rU   r7   rG   r6   r#   r#   r$   Útest_connect_ipv6_ipv6q   ó   z$TCPClientTest.test_connect_ipv6_ipv6c                 C   s4   |   ¡  t ¡ j d¡r|  d¡ |  tjd¡ d S )NZTwistedResolverz3TwistedResolver does not support multiple addressesr   )	rI   r   Zconfigured_classr.   Úendswithr;   rU   r7   rG   r6   r#   r#   r$   Útest_connect_ipv6_dualv   s   
z$TCPClientTest.test_connect_ipv6_dualc                 C   rV   rW   ©rU   r7   r8   r6   r#   r#   r$   Útest_connect_unspec_ipv4}   r\   z&TCPClientTest.test_connect_unspec_ipv4c                 C   r_   r`   )rI   rU   r7   r8   r6   r#   r#   r$   Útest_connect_unspec_ipv6€   rb   z&TCPClientTest.test_connect_unspec_ipv6c                 C   rV   r]   re   r6   r#   r#   r$   Útest_connect_unspec_dual…   r\   z&TCPClientTest.test_connect_unspec_dualc                 c   sV    t ƒ \}}|  |¡ |  t¡ | j d|¡V  W d   ƒ d S 1 s$w   Y  d S rW   )r   Z
addCleanupÚassertRaisesÚIOErrorr5   rN   )r   Zcleanup_funcr   r#   r#   r$   Útest_refused_ipv4ˆ   s   €

"ÿzTCPClientTest.test_refused_ipv4c                 C   s   | j tj| jtjddd dS )z8Fail when trying to use the source IP Address '8.8.8.8'.rX   z8.8.8.8©rJ   N)ri   r7   ÚerrorrU   rZ   r6   r#   r#   r$   Útest_source_ip_fail   s   
ûz!TCPClientTest.test_source_ip_failc                 C   s   | j tjddd dS )z=Success when trying to use the source IP Address '127.0.0.1'.rX   rl   NrY   r6   r#   r#   r$   Útest_source_ip_success™   s   z$TCPClientTest.test_source_ip_successc                 C   s4   t  ¡ dkr|  d¡ | jtj| jtjddd dS )z&Fail when trying to use source port 1.Úrootzrunning as rootrX   r   )rK   N)ÚgetpassÚgetuserr;   ri   r7   rm   rU   rZ   r6   r#   r#   r$   Útest_source_port_fail   s   

ûz#TCPClientTest.test_source_port_failc                 c   s`    d}G dd„ dt ƒ}|  t¡ t|ƒ djdd|dV  W d   ƒ d S 1 s)w   Y  d S )Ngš™™™™™©?c                   @   s   e Zd Zdd„ ZdS )z;TCPClientTest.test_connect_timeout.<locals>.TimeoutResolverc                 _   s   t ƒ S r%   r   )r   ÚargsÚkwargsr#   r#   r$   r@   ±   s   zCTCPClientTest.test_connect_timeout.<locals>.TimeoutResolver.resolveN)r.   r/   r0   r@   r#   r#   r#   r$   ÚTimeoutResolver°   s    rv   )Úresolverz1.2.3.4i90  )Útimeout)r   ri   r   r   rN   )r   rx   rv   r#   r#   r$   Útest_connect_timeout¬   s   €
ÿ"ÿz"TCPClientTest.test_connect_timeout)NN)r.   r/   r0   r3   r<   r=   r>   rI   r   rU   r[   r^   r   ra   rd   rf   rg   rh   rk   rn   ro   r   rs   ry   r1   r#   r#   r!   r$   r2   <   s4    





r2   c                   @   ó   e Zd Zdd„ Zdd„ ZdS )ÚTestConnectorSplitc                 C   sB   t  tdftdfg¡\}}|  |tdftdfg¡ |  |g ¡ d S )NÚaÚb)r	   ÚsplitÚAF1rR   ©r   ÚprimaryZ	secondaryr#   r#   r$   Útest_one_family»   s   z"TestConnectorSplit.test_one_familyc                 C   sZ   t  tdftdftdftdfg¡\}}|  |tdftdfg¡ |  |tdftdfg¡ d S ©Nr|   r}   ÚcÚd)r	   r~   r   ÚAF2rR   r€   r#   r#   r$   Ú
test_mixedÁ   s
   ÿzTestConnectorSplit.test_mixedN)r.   r/   r0   r‚   r‡   r#   r#   r#   r$   r{   º   s    r{   c                       sà   e Zd ZG dd„ deƒZ‡ fdd„Z‡ f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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/d0„ Z‡  ZS )1ÚConnectorTestc                   @   rz   )zConnectorTest.FakeStreamc                 C   ó
   d| _ d S )NF©Úclosedr6   r#   r#   r$   r   Ë   ó   
z!ConnectorTest.FakeStream.__init__c                 C   r‰   )NTrŠ   r6   r#   r#   r$   r,   Î   rŒ   zConnectorTest.FakeStream.closeN)r.   r/   r0   r   r,   r#   r#   r#   r$   Ú
FakeStreamÊ   s    r   c                    s8   t ƒ  ¡  i | _i | _tdftdftdftdfg| _d S rƒ   )r   r3   Úconnect_futuresr   r   r†   rH   r6   r!   r#   r$   r3   Ñ   s
   
ÿ"zConnectorTest.setUpc                    s*   | j  ¡ D ]}|  |j¡ qtƒ  ¡  d S r%   )r   ÚvaluesÚassertFalser‹   r   r>   r-   r!   r#   r$   r>   Ù   s   zConnectorTest.tearDownc                 C   s.   t  ¡ }|| j|< tƒ }|| j||f< ||fS r%   )rˆ   r   r   r   rŽ   )r   rL   rC   r(   Úfuturer#   r#   r$   Úcreate_streamà   s
   
zConnectorTest.create_streamc                 G   s   |   t| j ¡ ƒt|ƒ¡ d S r%   )rR   ÚsortedrŽ   Úkeys)r   r”   r#   r#   r$   Úassert_pendingç   s   zConnectorTest.assert_pendingc                 C   sX   | j  ||f¡}|r| | j| ¡ n| j |¡ | tƒ ¡ | j | j¡ |  	¡  d S r%   )
rŽ   ÚpopZ
set_resultr   Zset_exceptionrj   rE   Zadd_callbackr+   Úwait)r   rL   rC   Úsuccessr‘   r#   r#   r$   Úresolve_connectê   s   zConnectorTest.resolve_connectc                 C   s   |j D ]}|  |j¡ qd S r%   )r   Ú
assertTruer‹   )r   Úconnr(   r#   r#   r$   Úassert_connector_streams_closedõ   s   
ÿz-ConnectorTest.assert_connector_streams_closedc                 C   s,   t || jƒ}|jd| j ¡ d d}||fS )Ni  )Úconnect_timeout)r	   r’   ÚstartrE   Útime)r   rH   r›   r‘   r#   r#   r$   Ústart_connectù   s   zConnectorTest.start_connectc                 C   sZ   |   | j¡\}}|  t| j ¡ ƒtdfg¡ |  tdd¡ |  | ¡ td| j	d f¡ d S )Nr|   T)
r    rH   rR   ÚlistrŽ   r”   r   r™   Úresultr   ©r   r›   r‘   r#   r#   r$   Útest_immediate_successÿ   s    z$ConnectorTest.test_immediate_successc                 C   sB   |   tdfg¡\}}|  tdf¡ |  tdd¡ |  t|j¡ d S )Nr|   F©r    r   r•   r™   ri   rj   r¢   r£   r#   r#   r$   Útest_immediate_failure  s   z$ConnectorTest.test_immediate_failurec                 C   sr   |   tdftdfg¡\}}|  tdf¡ |  tdd¡ |  tdf¡ |  tdd¡ |  | ¡ td| jd f¡ d S ©Nr|   r}   FT)r    r   r•   r™   rR   r¢   r   r£   r#   r#   r$   Útest_one_family_second_try  s    z(ConnectorTest.test_one_family_second_tryc                 C   sd   |   tdftdfg¡\}}|  tdf¡ |  tdd¡ |  tdf¡ |  tdd¡ |  t|j¡ d S )Nr|   r}   Fr¥   r£   r#   r#   r$   Ú"test_one_family_second_try_failure  s   z0ConnectorTest.test_one_family_second_try_failurec                 C   sˆ   |   tdftdfg¡\}}|  tdf¡ | ¡  |  tdf¡ |  tdd¡ |  tdf¡ |  tdd¡ |  | ¡ td| jd f¡ d S r§   )r    r   r•   Ú
on_timeoutr™   rR   r¢   r   r£   r#   r#   r$   Ú"test_one_family_second_try_timeout  s    z0ConnectorTest.test_one_family_second_try_timeoutc                 C   s|   |   | j¡\}}|  tdf¡ |  tdd¡ |  tdftdf¡ |  tdd¡ |  tdd¡ |  | ¡ td| jd f¡ d S )Nr|   Fr}   r„   T)	r    rH   r•   r   r™   r†   rR   r¢   r   r£   r#   r#   r$   Ú#test_two_families_immediate_failure(  s    z1ConnectorTest.test_two_families_immediate_failurec                 C   s~   |   | j¡\}}|  tdf¡ | ¡  |  tdftdf¡ |  tdd¡ |  | ¡ td| j	d f¡ |  tdd¡ |  ¡  d S )Nr|   r„   TF)
r    rH   r•   r   rª   r†   r™   rR   r¢   r   r£   r#   r#   r$   Útest_two_families_timeout1  s   z'ConnectorTest.test_two_families_timeoutc                 C   sŠ   |   | j¡\}}|  tdf¡ | ¡  |  tdftdf¡ |  tdd¡ |  | ¡ td| j	d f¡ |  tdd¡ |  
| j	 d¡j¡ d S )Nr|   r„   T)r    rH   r•   r   rª   r†   r™   rR   r¢   r   rš   r–   r‹   r£   r#   r#   r$   Útest_success_after_timeout<  s   z(ConnectorTest.test_success_after_timeoutc                 C   sÂ   |   | j¡\}}|  tdf¡ | ¡  |  tdftdf¡ |  tdd¡ |  tdftdf¡ |  tdd¡ |  tdf¡ |  tdd¡ |  tdf¡ |  | ¡ ¡ |  tdd¡ |  	t
|j¡ d S )Nr|   r„   Fr…   r}   )r    rH   r•   r   rª   r†   r™   r   Údoneri   rj   r¢   r£   r#   r#   r$   Útest_all_failG  s   zConnectorTest.test_all_failc                 C   s’   |   tdftdfg¡\}}|  tdf¡ | ¡  | j tdf¡ |  | j d¡j¡ | 	¡  |  ¡  |  
t|jƒd¡ |  |¡ |  t|j¡ d S )Nr|   r}   r   )r    r   r•   Úon_connect_timeoutrŽ   r–   rš   r   r‹   rª   rR   Úlenrœ   ri   r   r¢   r£   r#   r#   r$   Ú-test_one_family_timeout_after_connect_timeoutW  s   
z;ConnectorTest.test_one_family_timeout_after_connect_timeoutc                 C   s–   |   tdftdfg¡\}}|  tdf¡ |  tdd¡ | ¡  |  ¡  |  | jd jd¡ |  t|jƒd¡ |  	|¡ |  | 
¡ td| jd f¡ d S )Nr|   r}   TFr   )r    r   r•   r™   r±   rR   r   r‹   r²   rœ   r¢   r£   r#   r#   r$   Ú.test_one_family_success_before_connect_timeoutg  s   
 z<ConnectorTest.test_one_family_success_before_connect_timeoutc                 C   s¦   |   tdftdfg¡\}}|  tdf¡ |  tdd¡ |  tdf¡ | ¡  | j tdf¡ |  | j d¡j	¡ |  ¡  |  
t|jƒd¡ |  |¡ |  t|j¡ d S ©Nr|   r}   Fr   )r    r   r•   r™   r±   rŽ   r–   rš   r   r‹   rR   r²   rœ   ri   r   r¢   r£   r#   r#   r$   Ú0test_one_family_second_try_after_connect_timeoutt  s   
z>ConnectorTest.test_one_family_second_try_after_connect_timeoutc                 C   s   |   tdftdfg¡\}}|  tdf¡ |  tdd¡ |  tdf¡ |  tdd¡ | ¡  |  ¡  |  t|jƒd¡ |  |¡ |  	t
|j¡ d S rµ   )r    r   r•   r™   r±   rR   r²   r   rœ   ri   rj   r¢   r£   r#   r#   r$   Ú9test_one_family_second_try_failure_before_connect_timeout  s   
zGConnectorTest.test_one_family_second_try_failure_before_connect_timeoutc                 C   sÀ   |   | j¡\}}|  tdf¡ | ¡  |  tdftdf¡ | ¡  | j tdf¡ |  	| j
 d¡j¡ | j tdf¡ |  	| j
 d¡j¡ |  ¡  |  t|j
ƒd¡ |  |¡ |  t|j¡ d S )Nr|   r„   r   )r    rH   r•   r   rª   r†   r±   rŽ   r–   rš   r   r‹   rR   r²   rœ   ri   r   r¢   r£   r#   r#   r$   Ú.test_two_family_timeout_before_connect_timeout  s   
z<ConnectorTest.test_two_family_timeout_before_connect_timeoutc                 C   s°   |   | j¡\}}|  tdf¡ | ¡  |  tdftdf¡ |  tdd¡ | j tdf¡ |  	| j
 d¡j¡ |  ¡  |  t|j
ƒd¡ |  |¡ |  | ¡ td| j
d f¡ d S )Nr|   r„   Tr   )r    rH   r•   r   rª   r†   r™   rŽ   r–   rš   r   r‹   rR   r²   rœ   r¢   r£   r#   r#   r$   Ú%test_two_family_success_after_timeoutœ  s   
 z3ConnectorTest.test_two_family_success_after_timeoutc                 C   s   |   | j¡\}}|  tdf¡ | ¡  | j tdf¡ |  | j d¡j	¡ |  ¡  | 
¡  |  ¡  |  t|jƒd¡ |  |¡ |  t|j¡ d S )Nr|   r   )r    rH   r•   r   r±   rŽ   r–   rš   r   r‹   rª   rR   r²   rœ   ri   r   r¢   r£   r#   r#   r$   Ú-test_two_family_timeout_after_connect_timeoutª  s   
z;ConnectorTest.test_two_family_timeout_after_connect_timeout)r.   r/   r0   Úobjectr   r3   r>   r’   r•   r™   rœ   r    r¤   r¦   r¨   r©   r«   r¬   r­   r®   r°   r³   r´   r¶   r·   r¸   r¹   rº   r1   r#   r#   r!   r$   rˆ   É   s2    	rˆ   ))Ú
contextlibr   rq   r9   r7   ZunittestZtornado.concurrentr   Ztornado.netutilr   r   Ztornado.queuesr   Ztornado.tcpclientr   r	   Ztornado.tcpserverr
   Ztornado.testingr   r   Ztornado.test.utilr   r   r   Ztornado.genr   ÚtypingÚTYPE_CHECKINGZtornado.iostreamr   r   r   r   r   r†   r   r2   ZTestCaser{   rˆ   r#   r#   r#   r$   Ú<module>   s,   ~