gfidZddlZddlZddlZddlZddlZddlZddlZddlZddl m Z ddl m Z ddl m Z ddl m Z ddl mZddl mZdd l mZdd l mZdd l mZdd l mZdd l mZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlm Z ddlm!Z!ddlm"Z"ddlm#Z#ddlm$Z$ddlm%Z%ddlm&Z&ddlm'Z'ddlm(Z(dd lm)Z)dd!lm*Z*dd"lm+Z+dd#lm,Z,ddl Z Gd$d%e'Z-Gd&d'e'Z.Gd(d)e'Z/Gd*d+e'Z0Gd,d-e'Z1d.Z2e)Gd/d0e'Z3e4d1krdd2l5m6Z6e6e7dSdS)3zContracts tests. These tests mainly check API sanity in terms of returned types and APIs availability. Some of these are duplicates of tests test_system.py and test_process.py N)AIX)BSD)FREEBSD)LINUX)MACOS)NETBSD)OPENBSD)OSX)POSIX)SUNOS)WINDOWS)FileNotFoundError)long)range)APPVEYOR)check_connection_ntuple) CI_TESTING)create_sockets)enum)GITHUB_ACTIONS) HAS_CPU_FREQ)HAS_NET_IO_COUNTERS)HAS_SENSORS_FANS)HAS_SENSORS_TEMPERATURES) is_namedtuple)kernel_version)process_namespace)PsutilTestCase)PYPY) serialrun) SKIP_SYSCONS)unittest)VALID_PROC_STATUSESc^eZdZdZdZdZdZeje oe ddZ dS)TestAvailConstantsAPIsc~|ttdtp tpt dS)N PROCFS_PATH) assertEqualhasattrpsutilrr rselfs }/builddir/build/BUILD/imunify360-venv-2.3.5/opt/imunify360/venv/lib64/python3.11/site-packages/psutil/tests/test_contracts.pytest_PROCFS_PATHz'TestAvailConstantsAPIs.test_PROCFS_PATHBs= 77.%.3 0 0 0 0 0c|j}|ttdt|ttdt|ttdt|ttdt|ttdt|ttdtdS)NABOVE_NORMAL_PRIORITY_CLASSBELOW_NORMAL_PRIORITY_CLASSHIGH_PRIORITY_CLASSIDLE_PRIORITY_CLASSNORMAL_PRIORITY_CLASSREALTIME_PRIORITY_CLASSr(r)r*r r,aes r-test_win_priorityz(TestAvailConstantsAPIs.test_win_priorityFs   768 9 97CCC 768 9 97CCC 760 1 17;;; 760 1 17;;; 762 3 3W=== 764 5 5w?????r/c4|j}|ttdt|ttdt|ttdt|ttdtdS)NIOPRIO_CLASS_NONEIOPRIO_CLASS_RTIOPRIO_CLASS_BEIOPRIO_CLASS_IDLEr(r)r*rr8s r-test_linux_ioprio_linuxz.TestAvailConstantsAPIs.test_linux_ioprio_linuxOs   76. / /777 76, - -u555 76, - -u555 76. / /77777r/c4|j}|ttdt|ttdt|ttdt|ttdtdS)N IOPRIO_HIGH IOPRIO_NORMAL IOPRIO_LOWIOPRIO_VERYLOWr7r8s r-test_linux_ioprio_windowsz0TestAvailConstantsAPIs.test_linux_ioprio_windowsVs   76= ) )7333 76? + +W555 76< ( ('222 76+ , ,g66666r/%unsupported on GITHUB_ACTIONS + LINUXc|j}|ttdtpt|ttdtpt|ttdtpt|ttdtpt|ttdtpt|ttdtpt|ttdtpt|ttdtpt|ttd tpt|ttd tpt|ttd tpt|ttd tt rt d kr$|ttdtt dkr$|ttdtt dkr$|ttdtt dkr$|ttdtt d kr$|ttdt|ttdt|ttdt|ttdtdS)N RLIM_INFINITY RLIMIT_AS RLIMIT_CORE RLIMIT_CPU RLIMIT_DATA RLIMIT_FSIZERLIMIT_MEMLOCK RLIMIT_NOFILE RLIMIT_NPROC RLIMIT_RSS RLIMIT_STACK RLIMIT_LOCKS)RLIMIT_MSGQUEUE)rVrW RLIMIT_NICE RLIMIT_RTPRIO)rVrW RLIMIT_RTTIMERLIMIT_SIGPENDING RLIMIT_SWAP RLIMIT_SBSIZE RLIMIT_NPTS)r(r)r*rrr rr8s r- test_rlimitz"TestAvailConstantsAPIs.test_rlimit]s  76? + +U-=g>>> 76; ' ')9'::: 76= ) )5+;G<<< 76< ( (%*:7;;; 76= ) )5+;G<<< 76> * *E,w??? 76? + +U-=g>>> 76> * *E, * *E, * *E222  @9,,76#455u===:--76=115999:--76?33U;;;:--76?33U;;;9,,76#677??? 76= ) )7333 76? + +W555 76= ) )733333r/N) __name__ __module__ __qualname__r.r:rArGr"skipIfrrrcr/r-r%r%@s000@@@888777X_^-<>>44>>444r/r%c2eZdZdZdZdZdZdZdZdS)TestAvailSystemAPIscb|ttdtdS)Nwin_service_iterr7r+s r-test_win_service_iterz)TestAvailSystemAPIs.test_win_service_iters) );<  F2 3 3U5Eg G G G G Gr/cb|ttdtdS)N sensors_fansr@r+s r-test_sensors_fansz%TestAvailSystemAPIs.test_sensors_fanss( 88%@@@@@r/c|ttdtptp t pt dS)Nsensors_battery)r(r)r*rr rrr+s r- test_batteryz TestAvailSystemAPIs.test_batterysC ):;;='=W= ? ? ? ? ?r/N) rdrerfrmrprsrvryr|rhr/r-rjrjszGGGFFF???GGGAAA?????r/rjceZdZdZdZdZdZdZej e oe ddZ dZ d Zd Zd Zd Zd ZdS)TestAvailProcessAPIsc|ttjdtp0t p)t p"tptptp tptdS)Nenviron) r(r)r*Processrrr rr rr rr+s r- test_environz!TestAvailProcessAPIs.test_environsi ;;6%676c6U6 6$+6/5 7 7 7 7 7r/cl|ttjdtdSNuidsr(r)r*rr r+s r- test_uidszTestAvailProcessAPIs.test_uids* 88%@@@@@r/cl|ttjdtdSrrr+s r- test_gidszTestAvailProcessAPIs.test_gidsrr/cl|ttjdtdS)Nterminalrr+s r- test_terminalz"TestAvailProcessAPIs.test_terminals* <>NN>>NCCCDDDJJJ666444IIIIIr/r~c|eZdZdZedZedfdZdZdZ dZ dZ e j e d d Zd Zd Ze j ed dZdZdZe j e d dZe j e d dZe j e d dZdZdZdS)TestSystemAPITypeszCheck the return types of system related APIs. Mainly we want to test we never return unicode on Python 2, see: https://github.com/giampaolo/psutil/issues/1039 c6tj|_dSN)r*rproc)clss r- setUpClasszTestSystemAPITypes.setUpClasss>##r/Tct|sJ|D]0}||||r||d1dSNr)rassertIsInstanceassertGreaterEqual)r,nttype_gezerons r-assert_ntuple_of_numsz(TestSystemAPITypes.assert_ntuple_of_numssdR      . .A  ! !!U + + + .''1--- . .r/c|tjtjdD]}||dS)NT)percpu)rr* cpu_times)r,rs r-test_cpu_timesz!TestSystemAPITypes.test_cpu_timess] ""6#3#5#5666"$/// + +B  & &r * * * * + +r/c|tjdt|tjdtdS)Nintervalgh㈵>)rr* cpu_percentfloatr+s r-test_cpu_percentz#TestSystemAPITypes.test_cpu_percentsP f0$???GGG f0'BBBEJJJJJr/c|tjd|tjddS)Nrg-C6?)rr*cpu_times_percentr+s r-test_cpu_times_percentz)TestSystemAPITypes.test_cpu_times_percentsL ""6#;T#J#J#JKKK ""6#;V#L#L#LMMMMMr/c^|tjtdSr)rr* cpu_countintr+s r-test_cpu_countz!TestSystemAPITypes.test_cpu_counts' f.00#66666r/z not supportedctj|d|tjtt t fdS)Nzcpu_freq() returns Noner)r*rrskipTestrrrrr+s r-rsz TestSystemAPITypes.test_cpu_freqsQ ?   $-- 9:: : ""6?#4#4UC>@@ O OKD%  ! !$ , , , O O%%dj#666%%dlUCd4LMMM%%di%d4jj1IJJJ%%dmeS$t**5MNNNN  O O Or/c^|tjtdSr)rr* boot_timerr+s r-test_boot_timez!TestSystemAPITypes.test_boot_time>s' f.00%88888r/ctjD]}||jt||jtt df||jtt df||jtt dfdSr) r*usersrrrrrhostpidr)r,users r- test_userszTestSystemAPITypes.test_usersBsLNN ? ?D  ! !$)S 1 1 1  ! !$-#tDzz1B C C C  ! !$)c4::-> ? ? ?  ! !$(S$t**,= > > > >  ? ?r/N)rdrerf__doc__ classmethodrrrrrrrr"rgrrsrrr!rrrrrrryrrvr rrhr/r-rrs $$[$/4D....+++ KKKNNN777X_%77PP87P === 555X_\?33++43+ I I I 1 1 1X_,,o>>//?>/ X_))?;;NN<;NX_11?CCOODCO999?????r/rcDeZdZeje ddZdS)TestProcessWaitTypez not POSIXcvtj|j}||}||tj t"| |tj dS| |tdSr) r*rspawn_testprocr terminatewaitr(signalSIGTERMrrrr)r,pcodes r-test_negative_signalz(TestProcessWaitType.test_negative_signalMs N4..004 5 5 vvxx ///    ! !$ 5 5 5 5 5  ! !$ , , , , ,r/N)rdrerfr"rgr rrhr/r-rrKs>X_Y ,,---,---r/rc t fd fd} tj ddg}n#tj$ricYSwxYw|d|dc d ji}t }||jdD]=\}} |||<#tj $r}| Yd}~6d}~wwxYw||S)Nc|j|j|t|tjr>|j6|jd|j|n/t|tjr |t||j sJdSr) r(rr isinstancer* ZombieProcessppidr NoSuchProcessassertProcessGonermsg)excrrr#rtcases r-check_exceptionz"proc_info..check_exceptionas #'3''' #(D))) c6/ 0 0 *x#((1555!!#(D111 V1 2 2 *  # #D ) ) ) Cwwr/cdkrC ddS#tj$r}|Yd}~dSd}~wwxYwdSr)rr*Error)r'r)rrr#rs r-do_waitzproc_info..do_waitmst !88 7 ! < 7 7 7T4666666666 7 8s AAAr#rrF) clear_cache) rr*ras_dictr$rritergettersr+) rr,drnsfunfun_namer'r)rr#rr(s ` @@@@@r- proc_infor5^sp   E      777777777~c"" LL&&) * *   6AfIJD$ 48 D 4 B?? X  SUUDNN|    OCtT 2 2 2 HHHH  GIII Ks)+AA%$A%3 CC(C##C(ceZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d Zd ZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZ dZ!d Z"d!Z#d"Z$d#Z%d$Z&d%Z'd&Z(d'S)(TestFetchAllProcesseszTest which iterates over all running processes and performs some sanity checks against Process API's returned values. Uses a process pool to get info about all processes. c6tj|_dSr)multiprocessingPoolpoolr+s r-setUpzTestFetchAllProcesses.setUps#(** r/cj|j|jdSr)r;rjoinr+s r-tearDownzTestFetchAllProcesses.tearDowns.  r/cfddlm}|j|t jS)Nr)r5)psutil.tests.test_contractsr5r;imap_unorderedr*pids)r,r5s r-iter_proc_infoz$TestFetchAllProcesses.iter_proc_infos4 :99999y'' 6;==AAAr/c `g}|D]}|D]\}}t||} ||||ddgddifvr |s J|9#t$rd}|d|d|ddt |d z }|d z }|d t jzz }d d |D}|d z }| |YwxYw|r(| d|dS) NrzH ====================================================================== z FAIL: test_z pid=rz, ret= zF----------------------------------------------------------------------z %sc3 K|] }d|zV dS)z Nrh).0is r- z1TestFetchAllProcesses.test_all..s&!H!HA7a-!H!H!H!H!H!Hr/) rDrgetattrAssertionErrorrepr traceback format_excr> splitlinesappendfail)r,failuresrrvaluemethss r-test_allzTestFetchAllProcesses.test_alls'')) , ,D#zz|| , , etT** ,D%%%QRr2$>>>$++e++u&'''.AAd5kkk4;;;;88AMA)"6"8"888A !H!H!H!H!HHHAIAOOA&&&&&'  ,  /))BGGH--.. . / /s A&&BD?Dc|||t|D]}||tdSr)rlistr)r,rrparts r-cmdlinezTestFetchAllProcesses.cmdlinesJ c4((( - -D  ! !$ , , , , - -r/c||ttdf|s||ddStr|dsdSt j|s J|trt j |rtt drxtt dre t j |t j sJdS#t$r.t j|rt sYdSYdSwxYwdSdSdSdS)NrGz.exeaccessX_OK)rrrr(r endswithospathisabsr isfiler)r_r`rNexistsrr,rrs r-exezTestFetchAllProcesses.exesq cCd#4555 "   S" % % % % % s||F33 7==%% * *s * *% ",, "2x(("WR-@-@""!ybg6666666)"""7>>#.."z"!"""""""  " " " """""s!D1D87D8ch||t||ddSrrrrrgs r-rzTestFetchAllProcesses.pid4 c3''' Q'''''r/cv||ttf||ddSr)rrrrrgs r-r#zTestFetchAllProcesses.ppids8 cC;/// Q'''''r/c||ttr|s|ddkrdSts|sJdSdS)Nstatusstopped)rrrrrgs r-rzTestFetchAllProcesses.namesZ c3'''  C DNi$?$? F JJ3   Jr/c||t ||dn/#t$r"tr|dt jkrnYnwxYwtjdtj |dS)Nrrnz%Y %m %d %H:%M:%S) rrrrNr r* STATUS_ZOMBIEtimestrftime localtimergs r- create_timez!TestFetchAllProcesses.create_times c5)))   # #C + + + +    4>V-AAA  )4>#+>+>?????s4)A A ct|sJ|D]3}||t||d4dSr)rrrr)r,rruids r-rzTestFetchAllProcesses.uidss^S!!!!! , ,C  ! !#s + + +  # #C + + + + , ,r/ct|sJ|D]A}||ttsts||dBdSr)rrrrrr)r,rrgids r-gidszTestFetchAllProcesses.gidssmS!!!!! 0 0C  ! !#s + + + 0 0''Q/// 0 0r/cD||t|sJdSr)rrrgs r-usernamezTestFetchAllProcesses.usernames% c3''' s r/c||t|sJ||d||tdS)N?)rrassertNotEqualassertInr#rgs r-rnzTestFetchAllProcesses.statussR c3''' s C%%% c./////r/ct|sJ|D]@}||ttf|dkr||dAdS)Nrrrrrr)r,rrfields r-rz!TestFetchAllProcesses.io_counterssiS!!!!! 2 2E  ! !%#t 5 5 5{{''q111 2 2r/ctrx||jt||jt||jd||jddSt jt jt j t j g}||t||d| ||dSr) rrioclassrrVrr*rFrErDrCr)r,rrchoicess r-rzTestFetchAllProcesses.ionice s  (  ! !#+s 3 3 3  ! !#)S 1 1 1  # #CK 3 3 3  # #CIq 1 1 1 1 1%!$" $G  ! !#s + + +  # #C + + + MM#w ' ' ' ' 'r/c||ttr|s|ddkrdS||ddS)Nrnro)rrrrrgs r- num_threadsz!TestFetchAllProcesses.num_threadssV c3'''  C DNi$?$? F Q'''''r/cX||t|D]}t|sJ||jd||jd||jd|D]$}||ttf%dSr) rr[rrid user_time system_timerr)r,rrtrs r-threadszTestFetchAllProcesses.threads"s c4((( ; ;A ## # ##  # #AD! , , ,  # #AK 3 3 3  # #AM1 5 5 5 ; ;%%ec5\:::: ;  ; ;r/ct|sJ|D]3}||t||d4dSr)rrrr)r,rrrs r-rzTestFetchAllProcesses.cpu_times,s^S!!!!! * *A  ! !!U + + +  # #Aq ) ) ) ) * *r/cn||td|cxkrdks nJ|dS)NrFgY@rrrgs r-rz!TestFetchAllProcesses.cpu_percent3sK c5)))c""""U"""""C"""""r/c b||ttr|dkrdS||dt jdkr||d||ttt jdS)Nrrr) rrrrr*rr(rr[rrgs r-rzTestFetchAllProcesses.cpu_num7s c3'''  sbyy F Q'''     " "   S! $ $ $ c4f&6&8&8 9 9::;;;;;r/ct|sJ|D]:}||ttf||d;t r||j|j||j|j ||j |j ||j |j dSdSr)rrrrrr peak_wsetwsetpeak_paged_pool paged_poolpeak_nonpaged_pool nonpaged_pool peak_pagefilepagefiler,rrrVs r- memory_infoz!TestFetchAllProcesses.memory_info@sS!!!!! . .E  ! !%#t 5 5 5  # #E1 - - - -  E  # #CM38 < < <  # #C$7 H H H  # #C$:C>#&& + + + +& ? + +r/c|D]K}||jt||jt||jt|jD]}t ||}|dkrI|ds3tj |jsJ|ja|dkr|sJt||dkr!ts|sJt|||ttf||dMdS)Nrc[rpermsr)rrrrrcrrM startswithrbrdrOr rrr)r,rrrfnamerVs r-rz!TestFetchAllProcesses.memory_mapssU 6 6B  ! !"'3 / / /  ! !"(C 0 0 0  ! !"'3 / / / 6 6E**F?? ++C00?!w}}RW55>>rw>>5f__ --$u++--5-g%%"2$11d5kk11u))%#t===++E15555 6  6 6r/ch||t||ddSrrjrgs r-rz!TestFetchAllProcesses.num_handlesrkr/c||ttrd|cxkrdks nJ|dSdttD}|||t jdkr"||tj dS||tdS)Nicbg|],}|dtt|-S)_PRIORITY_CLASS)rarMr*)rJxs r- z.TestFetchAllProcesses.nice..sC<<<ZZ(9::<'&!,,<<rs+   ,,,,,, !!!!!!000000######''''''''''''%%%%%%,,,,,,))))))111111&&&&&&''''''******''''''""""""%%%%%%!!!!!!,,,,,, <4<4<4<4<4^<4<4<4~?????.???0-I-I-I-I-I>-I-I-Ij|?|?|?|?|?|?|?|?~ - - - - -. - - -&(((V Q*Q*Q*Q*Q*NQ*Q* Q*h  z111111M(r/