Your IP : 3.21.105.46


Current Path : /usr/lib64/python3.6/site-packages/psutil/tests/__pycache__/
Upload File :
Current File : //usr/lib64/python3.6/site-packages/psutil/tests/__pycache__/test_connections.cpython-36.pyc

3

��JZ�P�@s8dZddlZddlZddlZddlmZddlmZddlmZddlmZddlm	Z	ddl
Z
ddl
mZdd	l
mZdd
l
m
Z
ddl
mZddl
mZdd
l
mZddl
mZddl
mZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlm Z ddlm!Z!ddlm"Z"ddlm#Z#ddlm$Z$ddlm%Z%dd lm&Z&dd!lm'Z'dd"lm(Z(dd#lm)Z)dd$lm*Z*e
j+�Z,Gd%d&�d&e-�Z.Gd'd(�d(e.e'j/�Z0Gd)d*�d*e.e'j/�Z1Gd+d,�d,e.e'j/�Z2Gd-d.�d.e'j/�Z3e4d/k�r4e!e5�dS)0z;Tests for net_connections() and Process.connections() APIs.�N)�closing)�AF_INET)�AF_INET6)�
SOCK_DGRAM)�SOCK_STREAM)�FREEBSD)�LINUX)�NETBSD)�OPENBSD)�OSX)�POSIX)�SUNOS)�WINDOWS)�
supports_ipv6)�PY3)�AF_UNIX)�bind_socket)�bind_unix_socket)�check_connection_ntuple)�create_sockets)�
get_free_port)�HAS_CONNECTIONS_UNIX)�pyrun)�
reap_children)�run_test_module_by_name)�safe_rmpath)�skip_on_access_denied)�tcp_socketpair)�TESTFN)�TRAVIS)�unittest)�unix_socket_path)�unix_socketpair)�
wait_for_filec@s8eZdZdd�Zdd�Zdd�Zd
dd	�Zddd�ZdS)�BasecCs"tstjdd�}|st|��dS)N�all)�kind)r	�thisproc�connections�AssertionError)�self�cons�r,�(/usr/lib64/python3.6/test_connections.py�setUp6sz
Base.setUpcCs0tt�t�ts,tjdd�}|s,t|��dS)Nr%)r&)rrrr	r'r(r))r*r+r,r,r-�tearDown<s
z
Base.tearDowncCsrtjdd�}tdd�|D��}tr.||j�S|jt|�d�|djdkrf|j||j�j|j��|dSdS)Nr%)r&cSsg|]}|j|f�qSr,)�fd)�.0�cr,r,r-�
<listcomp>Gsz+Base.get_conn_from_sock.<locals>.<listcomp>�r���)r'r(�dictr	�fileno�assertEqual�lenr0)r*�sockr+Zsmapr,r,r-�get_conn_from_sockEszBase.get_conn_from_sockNcCs�|dkr|j|�}t|�|jdkr6|j|j|j��|j|j|j�|j|j|jtj	tj
��|j�}|r�tr�t
|t�r�|j�}|jtkr�|dd�}|jtkr�tr�n|j|j|�|jtkr�tr�tjdd�}|jtj�|�|S)z�Given a socket, makes sure it matches the one obtained
        via psutil. It assumes this process created one connection
        only (the one supposed to be checked).
        Nr4�r%)r&r5)r;rr0r8r7�family�typeZ
getsockopt�socketZ
SOL_SOCKETZSO_TYPEZgetsocknamer�
isinstance�bytes�decoderrr
�laddrrr'r(�compare_procsys_connections�os�getpid)r*r:�connrCr+r,r,r-�check_socketRs(


zBase.check_socketr%csdytj|d�}Wn tjk
r0tr*dS�YnX�fdd�|D�}|j�|j�|j||�dS)z�Given a process PID and its list of connections compare
        those against system-wide connections retrieved via
        psutil.net_connections.
        )r&Ncs"g|]}|j�kr|dd��qS)Nr4r5)�pid)r1r2)rIr,r-r3�sz4Base.compare_procsys_connections.<locals>.<listcomp>)�psutil�net_connectionsZAccessDeniedr�sortr8)r*rIZ	proc_consr&Zsys_consr,)rIr-rDvsz Base.compare_procsys_connections)N)r%)�__name__�
__module__�__qualname__r.r/r;rHrDr,r,r,r-r$4s
	
$r$c@s|eZdZdZdd�Zeje�d�dd��Zdd�Z	eje�d�d	d
��Z
ejed�dd
��Zejed�dd��Z
dS)�TestUnconnectedSocketsz;Tests sockets which are open but not connected to anything.c
CsRdt�f}tttt|d���,}|j|�}|js4t�|j|j	t
j�WdQRXdS)Nz	127.0.0.1)�addr)rrrrrrH�raddrr)r8�statusrJ�CONN_LISTEN)r*rQr:rGr,r,r-�test_tcp_v4�s


z"TestUnconnectedSockets.test_tcp_v4zIPv6 not supportedc
CsRdt�f}tttt|d���,}|j|�}|js4t�|j|j	t
j�WdQRXdS)Nz::1)rQ)rrrrrrHrRr)r8rSrJrT)r*rQr:rGr,r,r-�test_tcp_v6�s


z"TestUnconnectedSockets.test_tcp_v6c
CsRdt�f}tttt|d���,}|j|�}|js4t�|j|j	t
j�WdQRXdS)Nz	127.0.0.1)rQ)rrrrrrHrRr)r8rSrJ�	CONN_NONE)r*rQr:rGr,r,r-�test_udp_v4�s


z"TestUnconnectedSockets.test_udp_v4c
CsRdt�f}tttt|d���,}|j|�}|js4t�|j|j	t
j�WdQRXdS)Nz::1)rQ)rrrrrrHrRr)r8rSrJrW)r*rQr:rGr,r,r-�test_udp_v6�s


z"TestUnconnectedSockets.test_udp_v6z
POSIX onlycCsXt��H}tt|td���,}|j|�}|js0t�|j|jt	j
�WdQRXWdQRXdS)N)r>)r!rrrrHrRr)r8rSrJrW)r*�namer:rGr,r,r-�
test_unix_tcp�s

z$TestUnconnectedSockets.test_unix_tcpcCsXt��H}tt|td���,}|j|�}|js0t�|j|jt	j
�WdQRXWdQRXdS)N)r>)r!rrrrHrRr)r8rSrJrW)r*rZr:rGr,r,r-�
test_unix_udp�s

z$TestUnconnectedSockets.test_unix_udpN)rMrNrO�__doc__rUr �skipIfrrVrXrYrr[r\r,r,r,r-rP�srPc@sTeZdZdZejed�dd��Zejed�dd��Z	e
ed�d	d
��Zdd�Z
d
S)�TestConnectedSocketPairszJTest socket pairs which are are actually connected to
    each other.
    zunreliable on SUONSc
Cs�dt�f}tjdd�st�tt|d�\}}zHtjdd�}|jt|�d�|j|djt	j
�|j|djt	j
�Wd|j�|j�XdS)Nz	127.0.0.1�tcp4)r&)rQr<rr4)rr'r(r)rrr8r9rSrJZCONN_ESTABLISHED�close)r*rQ�server�clientr+r,r,r-�test_tcp�s
z!TestConnectedSocketPairs.test_tcpz
POSIX onlycCslt���Z}t|�\}}�z2tjdd�}|djo8|djs@t�|djoR|djsZt�trldd�|D�}|jt	|�d�t
s�ts�tr�|j|djd�|j|djd�|j||djp�|dj�n~t
�rxv|dj|dj|dj|djfD]}|j|d�q�Wn<|j|dj�p$|dj|�|j|dj�pB|dj|�Wd|j�|j�XWdQRXdS)	N�unix)r&rr4cSsg|]}|jdkr|�qS)z/var/run/log)rR)r1r2r,r,r-r3�sz6TestConnectedSocketPairs.test_unix.<locals>.<listcomp>r<�)r!r"r'r(rCrRr)r	r8r9rrr
r
ra)r*rZrbrcr+rQr,r,r-�	test_unix�s*
"z"TestConnectedSocketPairs.test_unix)Zonly_ifcs�fdd�}tjd�}tjd�}ddlm}tjjt�}||�jt	t
�d|d�}||�jt	t
�d|d�}||�jt	t�d	|d�}||�jt	t�d	|d�}	t|�}
t
t|��}t|�}t
t|��}
t�r�t|�}t
t|��}t|	�}t
t|��}nd}d}d}d}�x�tj�D�]�}|j�}�jt|�d
�x�|D]�}|j|
jk�r\|||t
t|ftjd�n�|j|jk�r�|||t
t|
ftjd�nZ|jt|dd�k�r�|||tt|ftjd�n,|jt|dd�k�r0|||tt|ftjd��q0W�qW�jt|jdd�dS)Nc
s�d}t|��j|j|��j|j|��j|j|��j|j|��j|j|�x8|D]0}	|j|	d�}
|	|krz|
s�t�qX|
sXt|
��qXWt	r��j
|j|g�dS)
Nr%�inet�inet4�inet6�tcpr`�tcp6�udp�udp4�udp6)r&)
r%rhrirjrkr`rlrmrnro)rr8r=r>rCrRrSr(r)rrDrI)�procrGr=r>rCrRrSZkindsZ	all_kindsr&r+)r*r,r-�
check_conns

z8TestConnectedSocketPairs.test_combos.<locals>.check_conna
            import socket, time
            s = socket.socket($family, socket.SOCK_STREAM)
            s.bind(('$addr', 0))
            s.listen(1)
            with open('$testfn', 'w') as f:
                f.write(str(s.getsockname()[:2]))
            time.sleep(60)
        z�
            import socket, time
            s = socket.socket($family, socket.SOCK_DGRAM)
            s.bind(('$addr', 0))
            with open('$testfn', 'w') as f:
                f.write(str(s.getsockname()[:2]))
            time.sleep(60)
        r)�Templatez	127.0.0.1)r=rQZtestfnz::1r4r%rhrirkr`rmrnrIrjrlroz???)r&)r%rhrirkr`)r%rhrirmrn)r%rhrjrkrl)r%rhrjrmro)�textwrap�dedent�stringrrrE�path�basenamerZ
substitute�intrrr�evalr#rr'Zchildrenr(r8r9rIrrJrTrrW�getattr�assertRaises�
ValueError)r*rqZtcp_templateZudp_templaterrZtestfileZ
tcp4_templateZ
udp4_templateZ
tcp6_templateZ
udp6_templateZ	tcp4_procZ	tcp4_addrZ	udp4_procZ	udp4_addrZ	tcp6_procZ	tcp6_addrZ	udp6_procZ	udp6_addr�pr+rGr,)r*r-�test_combossb
z$TestConnectedSocketPairs.test_comboscCs�t����}tjdd�}|jt|�t|��tjdd�}|jt|�t�rJdnd�x,|D]$}|j|jtt	f�|j|j
t�qVWtjdd�}|jt|�d�|j|djt�|j|dj
t�t��rtjdd�}|jt|�d�|j|djt	�|j|dj
t�tjd	d�}|jt|�t��r(dnd�x.|D]&}|j|jtt	f�|j|j
t��q4Wtjd
d�}|jt|�d�|j|djt�|j|dj
t�t��r�tjdd�}|jt|�d�|j|djt	�|j|dj
t�tjdd�}|jt|�t��rd
nd�x2|D]*}|j|jtt	f�|j|j
ttf��qWt��r�tjdd�}|jt|�d�x.|D]&}|j|jt	�|j|j
ttf��qlWt
�r�tjdd�}|jt|�d�x.|D]&}|j|jt�|j|j
ttf��q�WWdQRXdS)Nr%)r&rkr<r4r`rrlrmrnrorh�rjre�)rr'r(r8r9r�assertInr=rrr>rrrr)r*�socksr+rGr,r,r-�test_multi_sockets_filteringes`





z5TestConnectedSocketPairs.test_multi_sockets_filteringN)rMrNrOr]r r^r
rdrrgrrr~r�r,r,r,r-r_�s
!br_c@sJeZdZdZe�dd��Ze�dd��Ze�eje	o6e
d�dd���Zd	S)
�TestSystemWideConnectionszTests for net_connections().c
s��fdd�}t���ddlm}xZ|j�D]N\}}|dkrBtrBq*|\}}tj|�}�jt|�tt	|���||||�q*W�j
ttjdd�WdQRXdS)NcsVttdt��}xB|D]:}�j|j||d�|j|krF�j|j||d�t|�qWdS)Nr)�msg)rzr?�objectr�r=r>r)r+�families�types_rrG)r*r,r-�check�s

z0TestSystemWideConnections.test_it.<locals>.checkr)�	conn_tmaprez???)r&)r�psutil._commonr��itemsrrJrKr8r9�setr{r|)r*r�r�r&�groupsr�r�r+r,)r*r-�test_it�s
z!TestSystemWideConnections.test_itcCs@t��0}dd�tjdd�D�}|jt|�t|��WdQRXdS)NcSsg|]}|jtj�kr|�qSr,)rIrErF)r1�xr,r,r-r3�sz>TestSystemWideConnections.test_multi_socks.<locals>.<listcomp>r%)r&)rrJrKr8r9)r*r�r+r,r,r-�test_multi_socks�sz*TestSystemWideConnections.test_multi_sockszunreliable on OSX + TRAVISc

st��}t|�}WdQRXg�d}xRt|�D]F}tjjt�t|�}tj	d|�}t
|�}�j|j�|j
t|�q,Wx$t|�D]}tt|�}t|�q�W�fdd�tjdd�D�}xJ�D]B�|jt�fdd�|D��|�tj��}	|jt|	jd��|�q�WdS)N�
a                import time, os
                from psutil.tests import create_sockets
                with create_sockets():
                    with open('%s', 'w') as f:
                        f.write(str(os.getpid()))
                    time.sleep(60)
                csg|]}|j�kr|�qSr,)rI)r1r�)�pidsr,r-r3�szFTestSystemWideConnections.test_multi_sockets_procs.<locals>.<listcomp>r%)r&csg|]}|j�kr|�qSr,)rI)r1r�)rIr,r-r3�s)rr9�rangerErv�realpathr�strrsrtr�appendrIZ
addCleanuprr#rJrKr8�Processr()
r*r�Zexpected�times�iZfname�srcZsprocZsysconsr}r,)rIr�r-�test_multi_sockets_procs�s(



z2TestSystemWideConnections.test_multi_sockets_procsN)rMrNrOr]rr�r�r r^rrr�r,r,r,r-r��s
r�c@seZdZdd�ZdS)�TestMisccCs�g}g}xhtt�D]\}|jd�rtt|�}t|�}|j�sBt|��|jt|�|j||�|j|�|j|�qWt	r�tj
tjtr�tj
dS)NZCONN_)�dirrJ�
startswithrzr��isupperr)ZassertNotInr�r
Z	CONN_IDLEZ
CONN_BOUNDrZCONN_DELETE_TCB)r*ZintsZstrsrZZnumZstr_r,r,r-�test_connection_constants�s 


z"TestMisc.test_connection_constantsN)rMrNrOr�r,r,r,r-r��sr��__main__)6r]rEr?rs�
contextlibrrrrrrJrrr	r
rrr
rr�rZpsutil._compatrZpsutil.testsrrrrrrrrrrrrrrrr r!r"r#r�r'r�r$ZTestCaserPr_r�r�rM�__file__r,r,r,r-�<module>s^\7`P


?>