U
    ‰d“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rÎd 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__© ú?/tmp/pip-unpacked-wheel-fekwu36z/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k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 k	r| j  ¡  d | _ 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|kr6|  d¡ d S )Nc                   S   s   t ƒ  dd¡S )Nr   éP   )r   Úresolver#   r#   r#   r$   Ú<lambda>U   ó    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   sv   |   |¡}| jj||||dV }| jd k	s.t‚| jj ¡ V }t|ƒ( | d¡ | 	d¡V }|  
|d¡ W 5 Q R X d S )N)Ú	source_ipÚsource_ports   helloé   )r<   r5   Úconnectr4   ÚAssertionErrorr   Úgetr   ÚwriteÚ
read_bytesÚassertEqual)	r   r    ÚhostrK   rL   r   r(   Zserver_streamÚdatar#   r#   r$   Údo_test_connectZ   s    
   ÿ

zTCPClientTest.do_test_connectc                 C   s   |   tjd¡ d S ©Nú	127.0.0.1©rV   r7   ÚAF_INETr6   r#   r#   r$   Útest_connect_ipv4_ipv4g   s    z$TCPClientTest.test_connect_ipv4_ipv4c                 C   s   |   tjd¡ d S ©Nr   rY   r6   r#   r#   r$   Útest_connect_ipv4_dualj   s    z$TCPClientTest.test_connect_ipv4_dualc                 C   s   |   ¡  |  tjd¡ d S ©Nz::1)rJ   rV   r7   rH   r6   r#   r#   r$   Útest_connect_ipv6_ipv6m   s    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   )	rJ   r   Zconfigured_classr.   Úendswithr;   rV   r7   rH   r6   r#   r#   r$   Útest_connect_ipv6_dualr   s    
z$TCPClientTest.test_connect_ipv6_dualc                 C   s   |   tjd¡ d S rW   ©rV   r7   r8   r6   r#   r#   r$   Útest_connect_unspec_ipv4y   s    z&TCPClientTest.test_connect_unspec_ipv4c                 C   s   |   ¡  |  tjd¡ d S r^   )rJ   rV   r7   r8   r6   r#   r#   r$   Útest_connect_unspec_ipv6|   s    z&TCPClientTest.test_connect_unspec_ipv6c                 C   s   |   tjd¡ d S r\   rb   r6   r#   r#   r$   Útest_connect_unspec_dual   s    z&TCPClientTest.test_connect_unspec_dualc              	   c   s>   t ƒ \}}|  |¡ |  t¡ | j d|¡V  W 5 Q R X 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©rK   N)rf   r7   ÚerrorrV   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   ri   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   )rL   N)ÚgetpassÚgetuserr;   rf   r7   rj   rV   rZ   r6   r#   r#   r$   Útest_source_port_fail™   s    
ûz#TCPClientTest.test_source_port_failc              	   c   sH   d}G dd„ dt ƒ}|  t¡  t|ƒ djdd|dV  W 5 Q R X 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   rs   )Úresolverz1.2.3.4i90  )Útimeout)r   rf   r   r   rN   )r   ru   rs   r#   r#   r$   Útest_connect_timeout¨   s      ÿz"TCPClientTest.test_connect_timeout)NN)r.   r/   r0   r3   r<   r=   r>   rJ   r   rV   r[   r]   r   r_   ra   rc   rd   re   rh   rk   rl   r   rp   rv   r1   r#   r#   r!   r$   r2   <   s2   





r2   c                   @   s   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ÚAF1rS   ©r   Z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 ©Nrx   ry   ÚcÚd)r	   rz   r{   ÚAF2rS   r|   r#   r#   r$   Ú
test_mixed½   s
    ÿzTestConnectorSplit.test_mixedN)r.   r/   r0   r}   r‚   r#   r#   r#   r$   rw   ¶   s   rw   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                   @   s   e Zd Zdd„ Zdd„ ZdS )zConnectorTest.FakeStreamc                 C   s
   d| _ d S )NF©Úclosedr6   r#   r#   r$   r   Ç   s    z!ConnectorTest.FakeStream.__init__c                 C   s
   d| _ d S )NTr„   r6   r#   r#   r$   r,   Ê   s    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   rI   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   ÚafrD   r(   Úfuturer#   r#   r$   Úcreate_streamÜ   s
    
zConnectorTest.create_streamc                 G   s   |   t| j ¡ ƒt|ƒ¡ d S r%   )rS   Ú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_exceptionrg   rF   Zadd_callbackr+   Úwait)r   rŠ   rD   Ú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Œ   ÚstartrF   Útime)r   rI   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 )Nrx   T)
rš   rI   rS   Ú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 )Nrx   F©rš   r{   r   r“   rf   rg   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 ©Nrx   ry   FT)rš   r{   r   r“   rS   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 )Nrx   ry   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“   rS   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 )Nrx   Fry   r   T)	rš   rI   r   r{   r“   r   rS   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 )Nrx   r   TF)
rš   rI   r   r{   r¤   r   r“   rS   rœ   r   r   r#   r#   r$   Útest_two_families_timeout-  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 )Nrx   r   T)rš   rI   r   r{   r¤   r   r“   rS   rœ   r   r”   r   r…   r   r#   r#   r$   Útest_success_after_timeout8  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 )Nrx   r   Fr€   ry   )rš   rI   r   r{   r¤   r   r“   r‰   Údonerf   rg   rœ   r   r#   r#   r$   Útest_all_failC  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 )Nrx   ry   r   )rš   r{   r   Úon_connect_timeoutr‡   r   r”   r   r…   r¤   rS   Úlenr–   rf   r   rœ   r   r#   r#   r$   Ú-test_one_family_timeout_after_connect_timeoutS  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 )Nrx   ry   TFr   )rš   r{   r   r“   r«   rS   r   r…   r¬   r–   rœ   r   r#   r#   r$   Ú.test_one_family_success_before_connect_timeoutc  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 ©Nrx   ry   Fr   )rš   r{   r   r“   r«   r‡   r   r”   r   r…   rS   r¬   r–   rf   r   rœ   r   r#   r#   r$   Ú0test_one_family_second_try_after_connect_timeoutp  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«   rS   r¬   r   r–   rf   rg   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 )Nrx   r   r   )rš   rI   r   r{   r¤   r   r«   r‡   r   r”   r   r…   rS   r¬   r–   rf   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 )Nrx   r   Tr   )rš   rI   r   r{   r¤   r   r“   r‡   r   r”   r   r…   rS   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 )Nrx   r   )rš   rI   r   r{   r«   r‡   r   r”   r   r…   r¤   rS   r¬   r–   rf   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ƒ   Å   s0   	rƒ   ))Ú
contextlibr   rn   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TestCaserw   rƒ   r#   r#   r#   r$   Ú<module>   s*   z