o
    ÙñhÏ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   Úadd_socketsÚgetsocknameÚport)ÚselfÚfamilyÚsockets©Ú	__class__© úO/var/www/vscode/kcb/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&   r5   =   s   
zTCPClientTest.setUpc                 C   s0   |t jkrdtjv r|  d¡ t|ƒ| _| jjS )NÚTRAVISz6dual-stack servers often have port conflicts on travis)ÚsocketÚ	AF_UNSPECÚosÚenvironÚskipTestr   r6   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'   )r6   r-   r8   r%   r%   r&   Ústop_serverH   s   


þzTCPClientTest.stop_serverc                    s    | j  ¡  |  ¡  tƒ  ¡  d S r'   )r7   r.   r@   r   ÚtearDownr8   r#   r%   r&   rA   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_loopÚrun_syncÚsetr:   ÚAF_INET6r>   )r    ÚaddrinfoÚ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?   r7   Úconnectr6   r   Úgetr   ÚwriteÚ
read_bytesÚassertEqual)	r    r!   ÚhostrO   rP   r   r*   Ú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©r[   r:   ÚAF_INETr8   r%   r%   r&   Útest_connect_ipv4_ipv4k   ó   z$TCPClientTest.test_connect_ipv4_ipv4c                 C   r\   ©Nr   r_   r8   r%   r%   r&   Útest_connect_ipv4_dualn   rb   z$TCPClientTest.test_connect_ipv4_dualc                 C   ó   |   ¡  |  tjd¡ d S ©Nz::1)rN   r[   r:   rK   r8   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 )NÚTwistedResolverz3TwistedResolver does not support multiple addressesr   )	rN   r   Úconfigured_classr0   Úendswithr>   r[   r:   rK   r8   r%   r%   r&   Útest_connect_ipv6_dualv   s   
z$TCPClientTest.test_connect_ipv6_dualc                 C   r\   r]   ©r[   r:   r;   r8   r%   r%   r&   Útest_connect_unspec_ipv4}   rb   z&TCPClientTest.test_connect_unspec_ipv4c                 C   re   rf   )rN   r[   r:   r;   r8   r%   r%   r&   Útest_connect_unspec_ipv6€   rh   z&TCPClientTest.test_connect_unspec_ipv6c                 C   r\   rc   rm   r8   r%   r%   r&   Útest_connect_unspec_dual…   rb   z&TCPClientTest.test_connect_unspec_dualc                 c   sV    t ƒ \}}|  |¡ |  t¡ | j d|¡V  W d   ƒ d S 1 s$w   Y  d S r]   )r   Ú
addCleanupÚassertRaisesÚIOErrorr7   rS   )r    Ú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'.r^   z8.8.8.8©rO   N)rr   r:   Úerrorr[   r`   r8   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'.r^   rv   Nr_   r8   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 rootr^   r   )rP   N)ÚgetpassÚgetuserr>   rr   r:   rw   r[   r`   r8   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&   rC   ±   s   zCTCPClientTest.test_connect_timeout.<locals>.TimeoutResolver.resolveN)r0   r1   r2   rC   r%   r%   r%   r&   ÚTimeoutResolver°   s    r€   )Úresolverz1.2.3.4i90  )Útimeout)r   rr   r   r   rS   )r    r‚   r€   r%   r%   r&   Útest_connect_timeout¬   s   €
ÿ"ÿz"TCPClientTest.test_connect_timeout)NN)r0   r1   r2   r5   r?   r@   rA   rN   r   r[   ra   rd   r   rg   rl   rn   ro   rp   ru   rx   ry   r   r}   rƒ   r3   r%   r%   r#   r&   r4   <   s4    





r4   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ÚAF1rW   ©r    ÚprimaryÚ	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‰   ÚAF2rW   rŠ   r%   r%   r&   Ú
test_mixedÁ   s
   ÿzTestConnectorSplit.test_mixedN)r0   r1   r2   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                   @   r„   )zConnectorTest.FakeStreamc                 C   ó
   d| _ d S )NF©Úclosedr8   r%   r%   r&   r   Ë   ó   
z!ConnectorTest.FakeStream.__init__c                 C   r”   )NTr•   r8   r%   r%   r&   r.   Î   r—   zConnectorTest.FakeStream.closeN)r0   r1   r2   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   r5   Úconnect_futuresr   r‰   r‘   rL   r8   r#   r%   r&   r5   Ñ   s
   
ÿ"zConnectorTest.setUpc                    s*   | j  ¡ D ]}|  |j¡ qtƒ  ¡  d S r'   )r   ÚvaluesÚassertFalser–   r   rA   r/   r#   r%   r&   rA   Ù   s   zConnectorTest.tearDownc                 C   s.   t  ¡ }|| j|< tƒ }|| j||f< ||fS r'   )r“   r˜   r   r   r™   )r    rQ   rF   r*   Úfuturer%   r%   r&   Úcreate_streamà   s
   
zConnectorTest.create_streamc                 G   s   |   t| j ¡ ƒt|ƒ¡ d S r'   )rW   Ú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™   ÚpopÚ
set_resultr   Úset_exceptionrs   rH   Úadd_callbackr-   Úwait)r    rQ   rF   Ú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   ÚstartrH   Útime)r    rL   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®   rL   rW   Ú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§   rr   rs   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§   rW   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§   rW   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®   rL   r    r‰   r§   r‘   rW   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®   rL   r    r‰   r¸   r‘   r§   rW   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®   rL   r    r‰   r¸   r‘   r§   rW   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®   rL   r    r‰   r¸   r‘   r§   r›   Údonerr   rs   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¸   rW   Úlenrª   rr   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¿   rW   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–   rW   rÀ   rª   rr   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¿   rW   rÀ   r   rª   rr   rs   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®   rL   r    r‰   r¸   r‘   r¿   r™   r¡   r¨   r   r–   rW   rÀ   rª   rr   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®   rL   r    r‰   r¸   r‘   r§   r™   r¡   r¨   r   r–   rW   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®   rL   r    r‰   r¿   r™   r¡   r¨   r   r–   r¸   rW   rÀ   rª   rr   r   r°   r±   r%   r%   r&   Ú-test_two_family_timeout_after_connect_timeoutª  s   
z;ConnectorTest.test_two_family_timeout_after_connect_timeout)r0   r1   r2   Úobjectr˜   r5   rA   r   r    r§   rª   r®   r²   r´   r¶   r·   r¹   rº   r»   r¼   r¾   rÁ   rÂ   rÄ   rÅ   rÆ   rÇ   rÈ   r3   r%   r%   r#   r&   r“   É   s2    	r“   ))Ú
contextlibr   r{   r<   r:   ÚunittestÚtornado.concurrentr   Útornado.netutilr   r   Útornado.queuesr   Útornado.tcpclientr   r	   Útornado.tcpserverr
   Útornado.testingr   r   Útornado.test.utilr   r   r   Útornado.genr   ÚtypingÚTYPE_CHECKINGÚtornado.iostreamr   r   r   r   r‰   r‘   r   r4   ÚTestCaser…   r“   r%   r%   r%   r&   Ú<module>   s,   ~