B
    u9ay,  ã               @   s²   d Z ddlZddlZddlmZ e d¡ ZddlZddlZddl	Z	e 
¡ dd„ ƒZG dd„ dejƒZG d	d
„ d
ejƒZG dd„ deƒZG dd„ deƒZdd„ Zedkr®eƒ  dS )z&Unit tests for socket timeout feature.é    N)ÚsupportÚnetworkc          	   C   s2   t  | ¡ t | |tjtj¡d d S Q R X dS )z—Resolve an (host, port) to an address.

    We must perform name resolution before timeout tests, otherwise it will be
    performed by connect().
    r   é   N)r   Útransient_internetÚsocketZgetaddrinfoÚAF_INETÚSOCK_STREAM)ZhostZport© r	   ú"/usr/lib/python3.7/test_timeout.pyÚresolve_address   s    r   c               @   sX   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S )ÚCreationTestCasez9Test case for socket.gettimeout() and socket.settimeout()c             C   s   t   t jt j¡| _d S )N)r   r   r   Úsock)Úselfr	   r	   r
   ÚsetUp   s    zCreationTestCase.setUpc             C   s   | j  ¡  d S )N)r   Úclose)r   r	   r	   r
   ÚtearDown!   s    zCreationTestCase.tearDownc             C   s   |   | j ¡ d d¡ d S )Nztimeout not disabled by default)ÚassertEqualr   Ú
gettimeout)r   r	   r	   r
   ÚtestObjectCreation$   s    z#CreationTestCase.testObjectCreationc             C   s^   | j  d¡ |  | j  ¡ d¡ | j  d¡ |  | j  ¡ d¡ | j  d ¡ |  | j  ¡ d ¡ d S )Ngáz®Ga@é   )r   Ú
settimeoutr   r   )r   r	   r	   r
   ÚtestFloatReturnValue)   s    z%CreationTestCase.testFloatReturnValuec             C   sP   | j  d¡ |  t| j  ¡ ƒtdƒ¡ | j  d¡ |  t| j  ¡ ƒtdƒ¡ d S )Né   g      ð?g333333@)r   r   r   Útyper   )r   r	   r	   r
   ÚtestReturnType4   s    zCreationTestCase.testReturnTypec             C   s    | j  d¡ | j  d¡ | j  d¡ | j  d ¡ |  t| j jd¡ |  t| j jd¡ |  t| j jd¡ |  t| j jg ¡ |  t| j ji ¡ |  t| j jd¡ d S )Nr   g        Ú r	   y                )r   r   ÚassertRaisesÚ	TypeError)r   r	   r	   r
   ÚtestTypeCheck<   s    zCreationTestCase.testTypeCheckc             C   s:   |   t| jjd¡ |   t| jjd¡ |   t| jjd¡ d S )Néÿÿÿÿg      ð¿)r   Ú
ValueErrorr   r   )r   r	   r	   r
   ÚtestRangeCheckI   s    zCreationTestCase.testRangeCheckc             C   s”   | j  d¡ | j  d¡ |  | j  ¡ d ¡ | j  d¡ |  | j  ¡ d¡ | j  d¡ | j  d¡ |  | j  ¡ d¡ | j  d¡ |  | j  ¡ d ¡ d S )Né
   r   r   g        )r   r   Úsetblockingr   r   )r   r	   r	   r
   ÚtestTimeoutThenBlockingO   s    z(CreationTestCase.testTimeoutThenBlockingc             C   sX   | j  d¡ | j  d¡ |  | j  ¡ d¡ | j  d¡ | j  d¡ |  | j  ¡ d¡ d S )Nr   r   )r   r#   r   r   r   )r   r	   r	   r
   ÚtestBlockingThenTimeout]   s    z(CreationTestCase.testBlockingThenTimeoutN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r!   r$   r%   r	   r	   r	   r
   r      s   r   c               @   s*   e Zd ZdZejZdd„ ZeZdd„ Z	dS )ÚTimeoutTestCaseg       @c             C   s
   t ƒ ‚d S )N)ÚNotImplementedError)r   r	   r	   r
   r   s   s    zTimeoutTestCase.setUpc       	      G   s¢   | j  |¡ t| j |ƒ}xbt|ƒD ]L}t ¡ }y||Ž  W q" tjk
rl } zt ¡ | }P W dd}~X Y q"X q"W |  d¡ |  	||| j
 ¡ |  ||d ¡ dS )z²
        Test the specified socket method.

        The method is run at most `count` times and must raise a socket.timeout
        within `timeout` + self.fuzz seconds.
        Nzsocket.timeout was not raisedg      ð?)r   r   ÚgetattrÚrangeÚtimeZ	monotonicr   ÚtimeoutZfailZ
assertLessÚfuzzZassertGreater)	r   Úcountr/   ÚmethodÚargsÚiZt1ÚeZdeltar	   r	   r
   Ú_sock_operationx   s    
zTimeoutTestCase._sock_operationN)
r&   r'   r(   r0   r   ZHOSTÚ	localhostr   r   r6   r	   r	   r	   r
   r*   h   s
   r*   c               @   sP   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S )ÚTCPTimeoutTestCasez3TCP test case for socket.socket() timeout functionsc             C   s"   t   t jt j¡| _tddƒ| _d S )Nzwww.python.org.éP   )r   r   r   r   r   Úaddr_remote)r   r	   r	   r
   r   ’   s    zTCPTimeoutTestCase.setUpc             C   s   | j  ¡  d S )N)r   r   )r   r	   r	   r
   r   –   s    zTCPTimeoutTestCase.tearDownc          
   C   s  t ddƒ}t ddƒ}d}t tjtj¡}d}| |¡ zZy| |¡ W nF tjk
r\   Y n2 tk
rŒ } z|jtj	kr|d}W d d }~X Y nX W d | 
¡  ~X |rÊ|  d |d	 |d
 ||d	 |d
 ¡¡ || _t | jd	 ¡ |  d
dd| j¡ W d Q R X d S )Nzblackhole.snakebite.netiZÝ  zwhitehole.snakebite.neti[Ý  Tr   FzÇWe didn't receive a connection reset (RST) packet from {}:{} within {} seconds, so we're unable to test connect timeout against the corresponding {}:{} (which is configured to silently drop packets).r   r   gü©ñÒMbP?Úconnect)r   r   r   r   r   r;   r/   ÚOSErrorÚerrnoZECONNREFUSEDr   ZskipTestÚformatr:   r   r   r6   )r   Z	blackholeZ	whiteholeÚskipr   r/   Úerrr	   r	   r
   ÚtestConnectTimeout™   s4    


z%TCPTimeoutTestCase.testConnectTimeoutc          	   C   s>   t  | jd ¡$ | j | j¡ |  dddd¡ W d Q R X d S )Nr   r   g      ø?Zrecvi   )r   r   r:   r   r;   r6   )r   r	   r	   r
   ÚtestRecvTimeoutí   s    z"TCPTimeoutTestCase.testRecvTimeoutc             C   s,   t  | j| j¡ | j ¡  |  ddd¡ d S )Nr   g      ø?Zaccept)r   Ú	bind_portr   r7   Úlistenr6   )r   r	   r	   r
   ÚtestAcceptTimeoutó   s    
z$TCPTimeoutTestCase.testAcceptTimeoutc          	   C   sZ   t   t jt j¡@}t || j¡ | ¡  | j | 	¡ ¡ |  
ddddd ¡ W d Q R X d S )Néd   g      ø?Úsendó   Xi@ )r   r   r   r   rC   r7   rD   r   r;   Úgetsocknamer6   )r   Úservr	   r	   r
   ÚtestSendù   s
    zTCPTimeoutTestCase.testSendc          	   C   s`   t   t jt j¡F}t || j¡ | ¡  | j | 	¡ ¡ |  
ddddd | 	¡ ¡ W d Q R X d S )NrF   g      ø?ZsendtorH   i@ )r   r   r   r   rC   r7   rD   r   r;   rI   r6   )r   rJ   r	   r	   r
   Ú
testSendto  s    zTCPTimeoutTestCase.testSendtoc          	   C   sZ   t   t jt j¡@}t || j¡ | ¡  | j | 	¡ ¡ |  
ddddd ¡ W d Q R X d S )NrF   g      ø?ZsendallrH   i@ )r   r   r   r   rC   r7   rD   r   r;   rI   r6   )r   rJ   r	   r	   r
   ÚtestSendall  s
    zTCPTimeoutTestCase.testSendallN)r&   r'   r(   r)   r   r   rA   rB   rE   rK   rL   rM   r	   r	   r	   r
   r8      s   T	
r8   c               @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚUDPTimeoutTestCasez3UDP test case for socket.socket() timeout functionsc             C   s   t   t jt j¡| _d S )N)r   r   Z
SOCK_DGRAMr   )r   r	   r	   r
   r     s    zUDPTimeoutTestCase.setUpc             C   s   | j  ¡  d S )N)r   r   )r   r	   r	   r
   r     s    zUDPTimeoutTestCase.tearDownc             C   s$   t  | j| j¡ |  dddd¡ d S )Nr   g      ø?Zrecvfromi   )r   rC   r   r7   r6   )r   r	   r	   r
   ÚtestRecvfromTimeout  s    z&UDPTimeoutTestCase.testRecvfromTimeoutN)r&   r'   r(   r)   r   r   rO   r	   r	   r	   r
   rN     s   rN   c               C   s   t  d¡ t  ttt¡ d S )Nr   )r   ZrequiresZrun_unittestr   r8   rN   r	   r	   r	   r
   Ú	test_main&  s
    
rP   Ú__main__)r)   Ú	functoolsZunittestZtestr   Zis_resource_enabledZskip_expectedr.   r=   r   Ú	lru_cacher   ZTestCaser   r*   r8   rN   rP   r&   r	   r	   r	   r
   Ú<module>   s    M' 