U 1gS@s\dZddlZddlZddlZddlmZddlmZddlmZddlmZddlm Z ddl Z ddl m Z dd l m Z dd l m Z dd l mZdd l mZdd l mZddl mZddl mZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlm Z ddlm!Z!ddlm"Z"ddlm#Z#ddlm$Z$ddlm%Z%dd lm&Z&dd!lm'Z'e(ed"e)Z*d#d$Z+e!j,j-d%d&Gd'd(d(eZ.Gd)d*d*e.Z/e!j,j-d%d&Gd+d,d,e.Z0e!j,j-d%d&Gd-d.d.e.Z1Gd/d0d0e.Z2e!j,j3ed1d2Gd3d4d4e.Z4Gd5d6d6eZ5dS)7zFTests for psutil.net_connections() and Process.net_connections() APIs.N)closing)AF_INET)AF_INET6) SOCK_DGRAM) SOCK_STREAM)FREEBSD)LINUX)MACOS)NETBSD)OPENBSD)POSIX)SUNOS)WINDOWS) supports_ipv6)PY3)AF_UNIX)HAS_NET_CONNECTIONS_UNIX) SKIP_SYSCONS)PsutilTestCase) bind_socket)bind_unix_socket)check_connection_ntuple)create_sockets)filter_proc_net_connections)pytest) reap_children)retry_on_failure)skip_on_access_denied)tcp_socketpair)unix_socketpair) wait_for_fileSOCK_SEQPACKETcCs$tj|d}|dkr t|S|S)Nkind)allunix)psutilProcessnet_connectionsr)r#consr*M/opt/hc_python/lib64/python3.8/site-packages/psutil/tests/test_connections.pythis_proc_net_connections2sr,serial)namec@s&eZdZddZddZd ddZdS) ConnectionTestCasecCstddgkstdSNr$r"r,AssertionErrorselfr*r*r+setUp;szConnectionTestCase.setUpcCstddgkstdSr0r1r3r*r*r+tearDown>szConnectionTestCase.tearDownr$csfztj|d}Wn"tjk r2tr,YdSYnXfdd|D}||||ksbtdS)zGiven a process PID and its list of connections compare those against system-wide connections retrieved via psutil.net_connections. r"Ncs"g|]}|jkr|ddqS)Npid.0cr8r*r+ Qs zBConnectionTestCase.compare_procsys_connections..)r&r(Z AccessDeniedr sortr2)r4r9Z proc_consr#Zsys_consr*r8r+compare_procsys_connectionsBsz.ConnectionTestCase.compare_procsys_connectionsN)r$)__name__ __module__ __qualname__r5r6r?r*r*r*r+r/9sr/c@s4eZdZejjeddddZddZddZ d S) TestBasicOperations requires rootreasonc Cs0t tjddD] }t|qW5QRXdSr0)rr&r(rr4connr*r*r+ test_systemXszTestBasicOperations.test_systemc Cs.ttddD] }t|qW5QRXdSr0)rr,rrGr*r*r+ test_process^sz TestBasicOperations.test_processc CsFtttddW5QRXtttjddW5QRXdS)Nz???r")rZraises ValueErrorr,r&r(r3r*r*r+test_invalid_kindcs  z%TestBasicOperations.test_invalid_kindN) r@rArBrmarkskipifrrIrJrLr*r*r*r+rCWs rCc@seZdZdZddZddZddZejj e dd d d Z d d Z ejj e dd ddZ ejj e dd ddZejj e dd ddZdS)TestUnconnectedSocketsz;Tests sockets which are open but not connected to anything.cCsttdd}tdd|D}ts$tr0||St|dks@t|djdkrh||j|ksht|dSdS)Nr$r"cSsg|]}|j|fqSr*)fdr:r*r*r+r=psz=TestUnconnectedSockets.get_conn_from_sock..rr7)r,dictr rfilenolenr2rP)r4sockr)Zsmapr*r*r+get_conn_from_sockns  z)TestUnconnectedSockets.get_conn_from_sockcCs||}t||jdkr.|j|ks.t|j|jks>t|j|tj tj ksXt| }|szt rzt |trz|}|jtkr|dd}|j|kst|jtkrtrtdd}|jt|dd|S)zGiven 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). r7Nr$r")rVrrPrSr2familytype getsockoptsocket SOL_SOCKETSO_TYPE getsocknamer isinstancebytesdecoderladdrrrr,r?osgetpid)r4rUrHrbr)r*r*r+ check_socket{s      z#TestUnconnectedSockets.check_socketc CsNd}tttt|d.}||}|jdks0t|jtj ks@tW5QRXdSN 127.0.0.1raddrr*) rrrrreraddrr2statusr& CONN_LISTENr4rjrUrHr*r*r+ test_tcp_v4s  z"TestUnconnectedSockets.test_tcp_v4zIPv6 not supportedrEc CsNd}tttt|d.}||}|jdks0t|jtj ks@tW5QRXdSN)::1rrir*) rrrrrerkr2rlr&rmrnr*r*r+ test_tcp_v6s  z"TestUnconnectedSockets.test_tcp_v6c CsNd}tttt|d.}||}|jdks0t|jtj ks@tW5QRXdSrf) rrrrrerkr2rlr& CONN_NONErnr*r*r+ test_udp_v4s  z"TestUnconnectedSockets.test_udp_v4c CsNd}tttt|d.}||}|jdks0t|jtj ks@tW5QRXdSrp) rrrrrerkr2rlr&rsrnr*r*r+ test_udp_v6s  z"TestUnconnectedSockets.test_udp_v6 POSIX onlyc CsP|}tt|td.}||}|jdks2t|jtj ksBtW5QRXdSN)rY get_testfnrrrrerkr2rlr&rsr4testfnrUrHr*r*r+ test_unix_tcps  z$TestUnconnectedSockets.test_unix_tcpc CsP|}tt|td.}||}|jdks2t|jtj ksBtW5QRXdSrwryr{r*r*r+ test_unix_udps  z$TestUnconnectedSockets.test_unix_udpN)r@rArB__doc__rVrerorrMrNrrrrtrur r}r~r*r*r*r+rOjs    rOc@sBeZdZdZejjeddddZejje ddddZ d S) TestConnectedSocketzFTest socket pairs which are actually connected to each other. zunreliable on SUONSrEcCsd}tddgksttt|d\}}zFtdd}t|dksBt|djtjksVt|djtjksjtW5||XdS)Nrgtcp4r"rirWrrQ) r,r2rrcloserTrlr&ZCONN_ESTABLISHED)r4rjserverclientr)r*r*r+test_tcps zTestConnectedSocket.test_tcprvcCs|}t|\}}ztdd}|djr<|djr.rWrx) rzrrr,rbrkr2r rrTrr r )r4r|rrr)r*r*r+ test_unixs    zTestConnectedSocket.test_unixN) r@rArBrrrMrNr rr rr*r*r*r+rs  rc@s.eZdZddZeedddZddZdS) TestFiltersc Csdd}t|dtttgtttg|dttgttg|dtgttg|dttgtg|dtgtg|dtgtg|d ttgtg|d tgtg|d tgtgtr|d tgtttgW5QRXdS) NcSsbt|dD] }|j|kst|j|ks tq ts^tj|dD] }|j|ksNt|j|ks.checkr$inetinet4tcprtcp6udpudp4udp6r%)rrrrrrr!r)r4rr*r*r+ test_filterss* zTestFilters.test_filters)Zonly_ifc stfdd}td}td}tjjtd}|jt t d|d}|jt t d|d}|jt t d|d}|jt t d|d} |} t t|d d } |} t t|d d } tr |} t t|d d } |}t t|d d }nd} d}d}d}tD]}|}t|d ks8t|D]}|j| jkrh|||t t| d tjd n|j| jkr|||t t| d tjdnZ|jt| ddkr|||t t|d tjdn,|jt|ddkr<|||t t|d tjdq<qdS)Nc sd}t||j|kst|j|ks(t|j|ks6t|j|ksDt|j|ksRt|D]2} |j| d} | |kr|| gkstqV| gksVtqVtr |j |gdS)N) r$rrinet6rrrrrrr") rrXr2rYrbrkrlr(rr?r9) procrHrXrYrbrkrlkindsZ all_kindsr#r)r3r*r+ check_conn$s  z+TestFilters.test_combos..check_conna4 import socket, time s = socket.socket({family}, socket.SOCK_STREAM) s.bind(('{addr}', 0)) s.listen(5) with open('{testfn}', 'w') as f: f.write(str(s.getsockname()[:2])) [time.sleep(0.1) for x in range(100)] a 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(0.1) for x in range(100)] )dirrh)rXrjr|rqT)deleterQr*)r$rrrr)r$rrrrr9)r$rrrr)r$rrrr)rtextwrapdedentrcpathbasenamerzgetcwdformatintrrpyrunevalr rr&r'childrenr(rTr2r9rrmrrsgetattr)r4rZ tcp_templateZ udp_templateZtestfileZ tcp4_templateZ udp4_templateZ tcp6_templateZ udp6_templateZ tcp4_procZ tcp4_addrZ udp4_procZ udp4_addrZ tcp6_procZ tcp6_addrZ udp6_procZ udp6_addrpr)rHr*r3r+ test_combos s         zTestFilters.test_combosc Csttdd}t|tr$dndks.t|D]$}|jttfksHt|jt ks2tq2tdd}t|dksrt|djtkst|djt ksttrtdd}t|dkst|djtkst|djt ksttdd}t|trdndkst|D]*}|jttfkst|jt kstqtd d}t|dksJt|djtks^t|djt ksrttrtd d}t|dkst|djtkst|djt ksttd d}t|trd ndkst|D].}|jttfkst|jt t fkstqtrntd d}t|dks>t|D]*}|jtksVt|jt t fksBtqBt rt st stdd}t|dkst|D]*}|jtkst|jt t fkstqW5QRXdS)Nrr"rWrQrrrrrrrrr%)rr,rTrr2rXrrrYrrrrr r)r4r)rHr*r*r+ test_counts\          zTestFilters.test_countN)r@rArBrrr rrr*r*r*r+rs   rrDrEc@s&eZdZdZddZeddZdS)TestSystemWideConnectionszTests for net_connections().c Csdd}thddlm}|D]L\}}|dkr:ts:q$|\}}t|}t|tt|ksdt ||||q$W5QRXdS)NcSs<|D]2}|j|kst|jtkr.|j|ks.tt|qdS)N)rXr2rrYr)r)rtypes_rHr*r*r+rs  z0TestSystemWideConnections.test_it..checkr) conn_tmapr%) rpsutil._commonritemsrr&r(rTsetr2)r4rrr#groupsrrr)r*r*r+test_its   z!TestSystemWideConnections.test_itc st}t|}W5QRXgd}g}t|D]:}|}||td|}||}|jq.|D] }t |qnfddt j ddD} D]Btfdd| D|kst t } t| d|kst qdS)N a" import time, os from psutil.tests import create_sockets with create_sockets(): with open(r'%s', 'w') as f: f.write("hello") [time.sleep(0.1) for x in range(100)] csg|]}|jkr|qSr*r8r;x)pidsr*r+r=s zFTestSystemWideConnections.test_multi_sockets_procs..r$r"csg|]}|jkr|qSr*r8rr8r*r+r=!s )rrTrangerzappendrrrr9r r&r(r2r') r4socksexpectedtimesfnames_fnamesrcZsprocZsysconsrr*)r9rr+test_multi_sockets_procss,       z2TestSystemWideConnections.test_multi_sockets_procsN)r@rArBrrrrr*r*r*r+rsrc@seZdZddZdS)TestMisccCsg}g}ttD]\}|drtt|}t|}|s@t|t|ksLt||ksXt||||qtr~tj tj t rtj dS)NZCONN_) rr& startswithrstrisupperr2rr Z CONN_IDLEZ CONN_BOUNDrZCONN_DELETE_TCB)r4Zintsstrsr.numZstr_r*r*r+test_net_connection_constants's        z&TestMisc.test_net_connection_constantsN)r@rArBrr*r*r*r+r&sr)6rrcr[r contextlibrrrrrr&rrr r r r r rrrZpsutil._compatrZ psutil.testsrrrrrrrrrrrrrrrr robjectr!r,rMZ xdist_groupr/rCrOrrrNrrr*r*r*r+sb                                 ] 7g?