gfShdZddlZddlZddlZddlZddlmZddlmZddlmZddlm Z ddlm Z ddl Z ddl m Z dd l m Z dd l mZdd l mZdd l mZdd l mZddl mZddl mZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlm Z ddlm!Z!ddlm"Z"ddlm#Z#ddlm$Z$ddlm%Z%ddlm&Z&dd lm'Z'dd!lm(Z(e j)Z*e+ed"e,Z-ej.dd#d$kZ/e"Gd%d&eZ0Gd'd(e0Z1e"Gd)d*e0Z2e"Gd+d,e0Z3Gd-d.e0Z4e&j5e$d/Gd0d1e0Z6Gd2d3eZ7e8d4krdd5l9m:Z:e:e;dSdS)6z;Tests for net_connections() and Process.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) bind_socket)bind_unix_socket)check_connection_ntuple)create_sockets)HAS_CONNECTIONS_UNIX)PsutilTestCase) reap_children)retry_on_failure) serialrun)skip_on_access_denied) SKIP_SYSCONS)tcp_socketpair)unittest)unix_socketpair) wait_for_fileSOCK_SEQPACKET) c"eZdZdZdZddZdS)ConnectionTestCasecxts.ts)td}|rJ|dSdSdSNallkind)r rthisproc connectionsselfconss /builddir/build/BUILD/imunify360-venv-2.3.5/opt/imunify360/venv/lib64/python3.11/site-packages/psutil/tests/test_connections.pysetUpzConnectionTestCase.setUp8sV "' "''U'33D ! !T ! !8 " " " " " !cxts.ts)td}|rJ|dSdSdSr))rr r-r.r/s r2tearDownzConnectionTestCase.tearDown>sX "6 "''U'33D ! !T ! !8  " " " " " !r4r*c  tj|}n#tj$r trYdSwxYwfd|D}|||||dS)zGiven a process PID and its list of connections compare those against system-wide connections retrieved via psutil.net_connections. r+Nc:g|]}|jk |ddS)Npid).0cr;s r2 zBConnectionTestCase.compare_procsys_connections..Ts(===q AcrcF r4)psutilnet_connections AccessDeniedr sort assertEqual)r0r; proc_consr,sys_conss ` r2compare_procsys_connectionsz.ConnectionTestCase.compare_procsys_connectionsEs  -4888HH"      >===H===  H-----s 44N)r*)__name__ __module__ __qualname__r3r6rFr4r2r'r'5sF""" """......r4r'cNeZdZejeddZdZdZdS)TestBasicOperations requires rootct5tjdD]}t| ddddS#1swxYwYdSr))rr?r@rr0conns r2 test_systemzTestBasicOperations.test_system\s    . ..E::: . .'---- . . . . . . . . . . . . . . . . . . .s(AA  A ct5tjdD]}t | ddddS#1swxYwYdSr))rr?Processr.rrOs r2 test_processz TestBasicOperations.test_processbs    . .((44%4@@ . .'---- . . . . . . . . . . . . . . . . . . .s:AAAc|ttjd|ttjddS)Nz???r+) assertRaises ValueErrorr-r.r?r@)r0s r2test_invalid_kindz%TestBasicOperations.test_invalid_kindgsD *h&:GGG *f&<5IIIIIr4N) rGrHrIrskipIfrrQrTrXrJr4r2rLrLZs_X_\?33..43. ... JJJJJr4rLc"eZdZdZdZdZdZeje ddZ dZ eje ddZ eje d d Zeje d d Zd S) TestUnconnectedSocketsz;Tests sockets which are open but not connected to anything.ctd}td|D}tstr||S|t|d|djdkrE|||j||dS)Nr*r+c g|] }|j|f SrJ)fdr<r=s r2r>z=TestUnconnectedSockets.get_conn_from_sock..rs---1adAY---r4rr9) r-r.dictr rfilenorClenr^)r0sockr1smaps r2get_conn_from_sockz)TestUnconnectedSockets.get_conn_from_sockps###//-----..  W  & &   SYY * * *AwzR  dkkmm!4!7GGG7Nr4c`||}t||jdkr-||j|||j|j||j|tj tj | }|s0tr)t|tr|}|jt kr |dd}|jt"krt$rn||j||jt"krKt(rDt*d}|t1j|d|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). r9Nr#r*r+)rfrr^rCrbfamilytype getsockoptsocket SOL_SOCKETSO_TYPE getsocknamer isinstancebytesdecoderrr laddrrr-r.rFosgetpid)r0rdrPrrr1s r2 check_socketz#TestUnconnectedSockets.check_socket}sy &&t,,%%% 7b==   TWdkkmm 4 4 4 dk222  Itv'8&.II K K K  "" # #E5!9!9 #LLNNE ;( " ""1"IE ;' ! !g !    TZ / / / ;' ! !&: !''U'33D  , ,RY[[$U , K K K r4cd}tttt|5}||}|jrJ||jtj ddddS#1swxYwYdSN 127.0.0.1raddr) rrrrruraddrrCstatusr? CONN_LISTENr0r{rdrPs r2 test_tcp_v4z"TestUnconnectedSockets.test_tcp_v4s [+DAAA B B >d$$T**Dz ! !>   T[&*< = = = > > > > > > > > > > > > > > > > > >AA==BBzIPv6 not supportedcd}tttt|5}||}|jrJ||jtj ddddS#1swxYwYdSN)::1rrz) rrrrrur|rCr}r?r~rs r2 test_tcp_v6z"TestUnconnectedSockets.test_tcp_v6s [;TBBB C C >t$$T**Dz ! !>   T[&*< = = = > > > > > > > > > > > > > > > > > >rcd}tttt|5}||}|jrJ||jtj ddddS#1swxYwYdSrw) rrrrrur|rCr}r? CONN_NONErs r2 test_udp_v4z"TestUnconnectedSockets.test_udp_v4s [*4@@@ A A   T[&*: ; ; ; < < < < < < < < < < < < < < < < <   T[&*: ; ; ; < < < < < < < < < < < < < < < < <   T[&*: ; ; ; < < < < < < < < < < < < < < < < < <AB  B B c,|}tt|t5}||}|jrJ||jtj ddddS#1swxYwYdSrrrs r2 test_unix_udpz$TestUnconnectedSockets.test_unix_udprrN)rGrHrI__doc__rfrurrrYrrrrr rrrJr4r2r[r[lsEE   !!!F>>>X_(*>??>>@?><<<X_(*>??<<@?<X_Y --<<.-<X_Y --<<.-<<z1TestConnectedSocket.test_unix..s$EEEa17n+D+D+D+D+Dr4r#msg)rr r-r.rrr|r rrCrcr rr r)r0rrrr1r{s r2 test_unixzTestConnectedSocket.test_unixs$""(00 ''V'44DQ 8$q'- 8 88Q 8$q'- 8 88 F FFE4EEE   SYYt  4 4 4 I I5 I  a333  a333  a)G$q'-HHHH I!!W]DGM!!W]DGM;//D$$T2..../   a!?$q'-HHH  a!?$q'-HHH LLNNN LLNNNNN LLNNN LLNNNNs GH*IN) rGrHrIrrrYrrr rrJr4r2rrsu X_U12232&X_Y --.-r4rcDeZdZdZeedZdZdS) TestFilterscfd}t5|dtttgtt t g|dttgtt g|dtgtt g|dttgtg|dtgtg|dtgtg|dttgt g|d tgt g|d tgt gtr%|d tgtt t gddddS#1swxYwYdS) Nc^t|D]8}|j||j|9t sNt j|D]:}|j||j|9dSdS)Nr+)r-r.assertInrhrirr?r@)r,familiestypesrPr0s r2checkz'TestFilters.test_filters..checks ,,$,77 0 0 dk8444 di//// 4"2===44DMM$+x888MM$)U3333 4 444r4r*inetinet4tcprtcp6udpudp4udp6r)rrrrrrr"r)r0rs` r2 test_filterszTestFilters.test_filterss 4 4 4 4 4   A A E%Hg. N; = = = E&H% + - - - E') + - - - E%H%- ! ! ! E&)- ! ! ! E&*- ! ! ! E%H%,    E&),    E&*,   $ Afi"J?AAA; A A A A A A A A A A A A A A A A A AsEE&&E*-E*)only_ifc tfd}tjd}tjd}tjtj}|ttd|}|ttd|}|ttd|}|ttd|} |} tt|d } |} tt|d } trg |} tt|d } |}tt|d }nd} d}d}d}t D].}|}t)|d |D]}|j| jkr'|||tt,| d t.jd 9|j| jkr'|||tt2| d t.jd p|jt7| ddkr'|||tt,|d t.jd|jt7|ddkr&|||tt2|d t.jd0dS)Ncd}t| |j| |j| |j| |j| |j||D]-} || } | |vr| sJ!| r J| .tr |j |gdSdS)N) r*rrinet6rrrrrrr+) rrCrhrirrr|r}r.rrFr;) procrPrhrirrr|r}kinds all_kindsr,r1r0s r2 check_connz+TestFilters.test_combos..check_conn>s8I #D ) ) )   T[& 1 1 1   TY - - -   TZ / / /   TZ / / /   T[& 1 1 1! * *''T'225==KK4K#))T))8)$ C00D6BBBBB C Cr4a 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(60) 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(60) )dirry)rhr{rrT)deleter`rJ)r*rrrr)r*rrrrr;)r*rrrr)r*rrrr)rtextwrapdedentrspathbasenamergetcwdformatintrrpyrunevalr!rr-childrenr.rCrcr;rr?r~rrgetattr)r0r tcp_template udp_templatetestfile tcp4_template udp4_template tcp6_template udp6_template tcp4_proc tcp4_addr udp4_proc udp4_addr tcp6_proc tcp6_addr udp6_proc udp6_addrpr1rPs` r2 test_comboszTestFilters.test_combos:sw C C C C C* (  ( 7##DOO O$D$DEE$++w<> JJ}-- x===>> ??  =11I]8DAAABBI =11I]8DAAABBIIIIII""$$ H HA==??D   SYY * * * H H5IM))Jq$i%1FHHHHUim++Jq$Y%/FHHHHUgi====Jq$+y"%1FHHHHUgi====Jq$*i%/FHHH% H H Hr4c t5td}|t |t rdnd|D]I}||jttf||j tJtd}|t |d||djt||dj tt rtd}|t |d||djt||dj ttd}|t |t rdnd|D]I}||jttf||j tJtd }|t |d||djt||dj tt rtd }|t |d||djt||dj ttd }|t |t rd nd|D]P}||jttf||j ttfQt rtd }|t |d|D]I}||jt||j ttfJtrtststd}|t |d|D]I}||jt ||j ttfJddddS#1swxYwYdS)Nrr+r#r`rrrrrrrrrr$)rr-r.rCrcrrrhrrrirrrrr r)r0r1rPs r2 test_countzTestFilters.test_counts   7 H7 H''U'33D   SYY]__(C! D D D 9 9 dkGX+>???  K8888''V'44D   SYY * * *   T!W^W 5 5 5   T!W\; 7 7 7 <+++88  TA...  a:::  a{;;;''U'33D   SYY]__(C! D D D 8 8 dkGX+>???  J7777''V'44D   SYY * * *   T!W^W 5 5 5   T!W\: 6 6 6 ;+++88  TA...  a:::  az:::''V'44D   SYY]__(C! D D D D D dkGX+>??? di+z)BCCCC H+++99  TA... HHD$$T[(;;;MM$)k:-FGGGG$ HW H H+++88  TA... HHD$$T[':::MM$)k:-FGGGGo7 H7 H7 H7 H7 H7 H7 H7 H7 H7 H7 H7 H7 H7 H7 H7 H7 H7 HsUU--U14U1N)rGrHrIrrr rrrJr4r2rr sl)A)A)AV5)))^H^H*)^H@8H8H8H8H8Hr4rrMc>eZdZdZdZedZdS)TestSystemWideConnectionszTests for net_connections().c fd}t5ddlm}|D]v\}}|dkrts|\}}t j|}t|tt|||||w ddddS#1swxYwYdS)Nc|D][}|j|||jtkr|j||t |\dS)Nr)rrhrrir)r1rtypes_rPr0s r2rz0TestSystemWideConnections.test_it..checksn . . dk8 >>>;'))MM$)VM>>>'----  . .r4r) conn_tmapr) rpsutil._commonritemsrr?r@rCrcset)r0rrr,groupsrrr1s` r2test_itz!TestSystemWideConnections.test_its, . . . . .   . . 0 0 0 0 0 0 ) 1 1 . . f6>>*>>#) &-d33  TCD NN;;;dHf---- . . . . . . . . . . . . . . . . . . .sBB55B9<B9c t5}t|}dddn #1swxYwYg d}g}t|D]q}|}||t jd|z}||} |jr|D]}t| fdtj dD} D]{ | t fd| D|tj } | t| d||dS)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(60) c&g|] }|jv |SrJr:)r<xpidss r2r>zFTestSystemWideConnections.test_multi_sockets_procs.. s;AEEE1EEEr4r*r+c*g|]}|jk |SrJr:)r<rr;s r2r>zFTestSystemWideConnections.test_multi_sockets_procs..s !E!E!E ! r4)rrcrangerappendrrrr;r!r?r@rCrSr.) r0socksexpectedtimesfnamesifnamesrcsprocsysconsrr;rs @@r2test_multi_sockets_procsz2TestSystemWideConnections.test_multi_sockets_procss   "5zzH " " " " " " " " " " " " " " "u # #AOO%%E MM% /##CJJsOOE KK " " " " ! !E % f4%@@@ B BC   S!E!E!E!EW!E!E!EFF% ' ' 's##A   Su!5!566 A A A A  B Bs -11N)rGrHrIrrrrrJr4r2rrsS&&...&$B$B$B$B$Br4rceZdZdZdS)TestMisccg}g}ttD]}|drtt|}t |}|s J||t||||||||trtj tj trtj dSdS)NCONN_) rr? startswithrstrisupper assertNotInrr CONN_IDLE CONN_BOUNDrCONN_DELETE_TCB)r0intsstrsnamenumstr_s r2test_connection_constantsz"TestMisc.test_connection_constantssKK " "Dw'' "fd++3xx||~~++t++~  d+++  d+++ C    D!!!          #  " " " " # #r4N)rGrHrIrrJr4r2rrs######r4r__main__) run_from_name)rs~BA  (((((( $$$$$$))))))000000''''''------''''''&&&&&&))))))""""""......%%%%%%''''''!!!!!!((((((&&&&&& 6>  !16688<<  RaR F *  !.!.!.!.!.!.!. !.HJJJJJ,JJJ$ _<_<_<_<_</_<_< _