3 JZK@s`dZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl m Z ddlmZddlmZddlmZddlmZddlmZdd lmZdd lmZdd lmZdd lmZdd lmZe jZe jdy$ddlZddlZddlZddlZWn$e k rDej!dkr@YnXWdQRXe j"j#Z#e j$d$kZ%ddZ&ej'e dGdddej(Z)ej'e dGdddej(Z*ej'e dGdddej(Z+ej'e dGdddej(Z,ej'e dGdddej(Z-ej'e dGdd d ej(Z.ej'e dGd!d"d"ej(Z/e0d#kr\ee1dS)%zWindows specific tests.N)WINDOWS)callable)APPVEYOR)get_test_subprocess) HAS_BATTERY)mock) reap_children)retry_before_failing)run_test_module_by_name)sh)unittestignorent csfdd}|S)Ncsty|f||Stk rn}zBddlm}|j|krDtjdd|jtjkr\tjddWYdd}~XnXdS)Nr)ACCESS_DENIED_SET)OSErrorZpsutil._pswindowsrerrnopsutil AccessDeniedZESRCH NoSuchProcess)selfargskwargserrr)fun$/usr/lib64/python3.6/test_windows.pywrapper5s     z wrap_exceptions..wrapperr)rrr)rrwrap_exceptions4s rz WINDOWS onlyc@seZdZddZejdejkdddZddZ d d Z d d Z eje d e ddZe ddZddZddZddZddZddZdS)TestSystemAPIscCsVtd}tjddj}x8|D]0}d|jddjkr8q||kr|jd|qWdS)Nz ipconfig /allT)Zperniczpseudo-interface -z-%r nic wasn't found in 'ipconfig /all' output)r rZnet_io_counterskeysreplacelowerfail)routZnicsZnicrrrtest_nic_namesJs zTestSystemAPIs.test_nic_namesNUMBER_OF_PROCESSORSz-NUMBER_OF_PROCESSORS env var is not availablecCs"ttjd}|j|tjdS)Nr))intosenviron assertEqualr cpu_count)rZnum_cpusrrrtest_cpu_countTszTestSystemAPIs.test_cpu_countcCs$tjd}tj}|j||dS)N)win32apiZ GetSystemInforr.r-)r sys_value psutil_valuerrrtest_cpu_count_2Zs zTestSystemAPIs.test_cpu_count_2cCs@tj}|jd}|j|jtjj|j|jtjj dS)Nr) wmiWMIZWin32_Processorr-ZCurrentClockSpeedrZcpu_freqZcurrentZ MaxClockSpeedmax)rwprocrrr test_cpu_freq_s zTestSystemAPIs.test_cpu_freqcCs,tjjd}|jt|jtjjdS)Nr) r5r6ZWin32_ComputerSystemr-r*ZTotalPhysicalMemoryrZvirtual_memorytotal)rr8rrrtest_total_phymemes z TestSystemAPIs.test_total_phymemztest not relieable on appveyorcCs:tjj}tdd|D}ttj}|j||dS)NcSsg|] }|jqSr) ProcessId).0xrrr |sz,TestSystemAPIs.test_pids..)r5r6 Win32_ProcesssetrZpidsr-)rr8Zwmi_pidsZ psutil_pidsrrr test_pidsvs  zTestSystemAPIs.test_pidscCstjdd}tjj}x|D]}x|D]}|jjdd|jkr(|jsHPytj |j}Wn4t k r}z|j t j krzPnWYdd}~XnX|j |jt|jt|j}|j |j|t|j|d kr|jd|j|fPq(W|jdt|qWdS) NT)all\ izpsutil=%s, wmi=%szcan't find partition %si(i)rdisk_partitionsr5r6ZWin32_LogicalDiskZdevicer$ZDeviceID mountpoint disk_usagerrENOENTr-r;r*ZSizeZ FreeSpacefreeabsr&repr)rZps_partsZ wmi_partsZps_partZwmi_partZusagerZwmi_freerrr test_diskss*      zTestSystemAPIs.test_diskscCspxjtjD]^}tj|j}tj|j}|j|d|jdd|j|d|jdd|j |j |j|jq WdS)Nri)deltaii) rrHr1ZGetDiskFreeSpaceExrIrJassertAlmostEqualrLr;r-Zused)rZdiskr2r3rrrtest_disk_usages  zTestSystemAPIs.test_disk_usagecCs>ddtjjdD}ddtjddD}|j||dS)NcSs$g|]}|r|jd r|dqS)zA:rE) startswith)r>r?rrrr@sz7TestSystemAPIs.test_disk_partitions..z\cSsg|] }|jqSr)rI)r>r?rrrr@sT)rD)r1ZGetLogicalDriveStringssplitrrHr-)rr2r3rrrtest_disk_partitionssz#TestSystemAPIs.test_disk_partitionscCs`ttj}tjj}t}x$|D]}|j|j|j|jq$W|j ||@d||fdS)Nzno common entries in %s, %s) rBcextZ net_if_statsr5r6ZWin32_NetworkAdapteraddNameZNetConnectionID assertTrue)rZps_namesZ wmi_adaptersZ wmi_namesZ wmi_adapterrrrtest_net_if_statss     z TestSystemAPIs.test_net_if_statscCs^tjj}|djjdd}tjj|d}tjjtj }t ||j }|j |ddS)Nr.z %Y%m%d%H%M%S) r5r6ZWin32_OperatingSystemZLastBootUpTimerUdatetimeZstrptimeZ fromtimestampr boot_timerMZ total_secondsZassertLessEqual)rZwmi_osZ wmi_btime_strZ wmi_btime_dtZ psutil_dtZdiffrrrtest_boot_times zTestSystemAPIs.test_boot_timecCstjddd|jtjdWdQRXtjddd|jtjdWdQRXtjddd|jtjdWdQRXtjddd|jtjdWdQRXdS)Nz psutil._pswindows.cext.boot_timer0) return_valueiM)rpatchr-rr_)rrrrtest_boot_time_fluctuationsz)TestSystemAPIs.test_boot_time_fluctuationN)__name__ __module__ __qualname__r(r skipIfr+r,r/r4r:r<rr rCrOrSrVr[r`rerrrrr Gs      r c@s`eZdZddZeje dddZeje dddZdd Z d d Z d d Z ddZ dS)TestSensorsBatterycCs.tjdr|jtjn|jtjdS)NZSystemBatteriesPresent)r1ZGetPwrCapabilitiesZassertIsNotNonersensors_battery assertIsNone)rrrrtest_has_batterys z#TestSensorsBattery.test_has_batteryz no batterycCs6tj}|jdd}tj}|j|j|jdddS)Nzselect * from Win32_BatteryrrQ)rP)r5r6queryrrkrRZpercentZEstimatedChargeRemaining)rr8 battery_wmibattery_psutilrrr test_percents zTestSensorsBattery.test_percentcCs6tj}|jdd}tj}|j|j|jdkdS)Nzselect * from Win32_Batteryrr)r5r6rnrrkr-Z power_pluggedZ BatteryStatus)rr8rorprrrtest_power_pluggeds z%TestSensorsBattery.test_power_pluggedc Cs,tjddd}|jtjWdQRXdS)Nz&psutil._pswindows.cext.sensors_batteryr)ra)rrsrr)rrdrlrrk)rmrrrtest_emulate_no_batterys z*TestSensorsBattery.test_emulate_no_batteryc Cs2tjddd}|jtjjtjWdQRXdS)Nz&psutil._pswindows.cext.sensors_batteryrQr)ra)rQrrr)rrdr-rrksecsleftPOWER_TIME_UNLIMITED)rrtrrrtest_emulate_power_connecteds   z/TestSensorsBattery.test_emulate_power_connectedc Cs2tjddd}|jtjjtjWdQRXdS)Nz&psutil._pswindows.cext.sensors_batteryr)ra)rryrr)rrdr-rrkrvrw)rrtrrrtest_emulate_power_chargings   z.TestSensorsBattery.test_emulate_power_chargingc Cs2tjddd}|jtjjtjWdQRXdS)Nz&psutil._pswindows.cext.sensors_batteryrrQ)ra)rrrr{)rrdr-rrkrvZPOWER_TIME_UNKNOWN)rrtrrrtest_emulate_secs_left_unknowns   z1TestSensorsBattery.test_emulate_secs_left_unknownN) rfrgrhrmr rirrqrrrurxrzr|rrrrrjs  rjc@seZdZeddZeddZddZddZd d Zd d Z d dZ ddZ ddZ e jejd+k dddZddZddZddZddZd d!Zd"d#Zd$d%Zd&d'Zd(d)Zd*S), TestProcesscCstj|_dS)N)rpid)clsrrr setUpClassszTestProcess.setUpClasscCs tdS)N)r)rrrr tearDownClassszTestProcess.tearDownClasscCstjd}|jtj|jdS)Nr)rProcess assertRaisesrkill)rprrr test_issue_24s zTestProcess.test_issue_24c Cstjd}|j|jdt||j|j|jdky|jdd\}}Wn(tj k r|t j dd krxYnX|j|d kdS) NrbZSystemgrrQvistawin-7win7r)rrr) rrr-namestrusernamerZ create_time memory_inforplatformuname)rrrssvmsrrrtest_special_pid s zTestProcess.test_special_pidcCs"tj|j}|jt|jtjdS)N)rrr~r ValueError send_signalsignalSIGINT)rrrrrtest_send_signal1s zTestProcess.test_send_signalc CsNxHtjD]<}y |jtjj|j|jWq tjk rDYq Xq WdS)N) r process_iterr-r+pathbasenameexerError)rrrrrtest_exe5s  zTestProcess.test_execCsbtjtj}|j}tjtjtj tj}|j}|j ||dtj ||j |j|dS)NrQ) rrr+getpid num_handlesr1 OpenProcesswin32conPROCESS_QUERY_INFORMATIONFALSEr- CloseHandle)rrZbeforehandleafterrrrtest_num_handles_increment<s z&TestProcess.test_num_handles_incrementc sfdd}tj|j}g}xttjD]jds(dkrBq(q(y(|||j}|||j}Wntjtjfk rYq(X||kr(d||f}|j|q(W|r|j ddj |dS)Ncs,t|d}|dk r$t|r$|n|dS)N)getattrr)rattr)rrrcallJs z+TestProcess.test_handles_leak..call_ terminatersuspendresumenicerwaitchildrenas_dictmemory_info_exz@failure while processing Process.%s method (before=%s, after=%s) ) rrrrrrrrrr) rrr~dirrTrrrappendr&join)rrrZfailuresZnum1Znum2r&r)rrtest_handles_leakFs,       zTestProcess.test_handles_leakc Cs:x4tjD](}y |jWq tjk r0Yq Xq WdS)N)rrrr)rrrrrtest_name_always_availablejs  z&TestProcess.test_name_always_availablerzCTRL_* signals not supportedcCsbtjtj}|jtj|jtj|j|j |j tj |jtj|j tj |jtjdS)N) rrrr~rrZ CTRL_C_EVENTZCTRL_BREAK_EVENTrrrr)rrrrrtest_ctrl_signalsss   zTestProcess.test_ctrl_signalsc Cs\xVtjD]J}ytjj|j}|j}Wntjtjfk rFYq X|j ||q WdS)N) rrr+rrrrrrr-)rrabrrrtest_compare_name_exes z!TestProcess.test_compare_name_execCs |jtjjtjtjdS)N)r-rrrr1Z GetUserNameExrZNameSamCompatible)rrrr test_usernameszTestProcess.test_usernamecCs8tjddtjj}djtjj}|j ||dS)Nz +r!) resubr1ZGetCommandLinestriprrrcmdliner-)rr2r3rrr test_cmdlineszTestProcess.test_cmdlinecCsJtjtjtjtj}|jtj|t j |}t j j }|j||dS)N)r1rrrrr+r addCleanupr win32processZGetPriorityClassrrrr-)rrr2r3rrr test_nices   zTestProcess.test_nicecCstjtjtj|j}|jtj|tj |}t j |jj }|j |d|j|j |d|j|j |d|j|j |d|j|j |d|j|j |d|j|j |d|j|j |d|j|j |j|j|j |j|jdS) NZPeakWorkingSetSizeWorkingSetSizeZQuotaPeakPagedPoolUsageZQuotaPagedPoolUsageZQuotaPeakNonPagedPoolUsageZQuotaNonPagedPoolUsageZ PagefileUsageZPeakPagefileUsage)r1rrrrr~rrrZGetProcessMemoryInforrrr-Z peak_wsetZwsetZpeak_paged_poolZ paged_poolZpeak_nonpaged_poolZ nonpaged_poolZpagefileZ peak_pagefilerr)rrr2r3rrrtest_memory_infos0  zTestProcess.test_memory_infocCsXtjtjtj|j}|jtj|tj |j}|j |j }t j |}|j||dS)N)r1rrrrr~rrrrrrrZGetExitCodeProcessr-)rrrr3r2rrr test_waits   zTestProcess.test_waitcCs\dd}tjtjtj|j}|jtj||tj |d}t j |jj }|j ||dS)NcsfddtdDS)Ncsg|]}d|>@r|qS)rQr)r>i)r?rrr@szGTestProcess.test_cpu_affinity..from_bitmask..@)range)r?r)r?r from_bitmasksz3TestProcess.test_cpu_affinity..from_bitmaskr)r1rrrrr~rrrZGetProcessAffinityMaskrrZ cpu_affinityr-)rrrr2r3rrrtest_cpu_affinitys zTestProcess.test_cpu_affinitycCstjtjtjtj}|jtj|t j |}t j j }|j|j|d|j|j|d|j|j|d|j|j|d|j|j|d|j|j|ddS)NZReadOperationCountZWriteOperationCountZReadTransferCountZWriteTransferCountZOtherOperationCountZOtherTransferCount)r1rrrrr+rrrrZGetProcessIoCountersrr io_countersr-Z read_countZ write_countZ read_bytesZ write_bytes other_countZ other_bytes)rrr2r3rrrtest_io_counterss"  zTestProcess.test_io_counterscCsddl}ddl}d}|jjj|dtj}|j|jjj||j j }|jjj ||j ||j }tjj}|jjj||j||ddS)NrirQ)ctypesZctypes.wintypesZwindllZkernel32rr+rrrZwintypesZDWORDZGetProcessHandleCountZbyrefvaluerrrr-)rrrrZhndcntr2r3rrrtest_num_handless  zTestProcess.test_num_handlesN)rr)rfrgrh classmethodrrrrrrrrrr risys version_inforrrrrrrrrrrrrrr}s(   $     r}c@s`eZdZdZeddZeddZddZdd Zd d Z d d Z ddZ ddZ ddZ dS)TestProcessWMIz%Compare Process API results with WMI.cCstj|_dS)N)rr~)rrrrrszTestProcessWMI.setUpClasscCs tdS)N)r)rrrrrszTestProcessWMI.tearDownClasscCs8tjj|jdd}tj|j}|j|j|jdS)N)r=r) r5r6rAr~rrr-rZCaption)rr8rrrr test_name s zTestProcessWMI.test_namecCs@tjj|jdd}tj|j}|j|jj|j jdS)N)r=r) r5r6rAr~rrr-rr%ZExecutablePath)rr8rrrrrs zTestProcessWMI.test_execCsFtjj|jdd}tj|j}|jdj|j|j j dddS)N)r=rr!"rF) r5r6rAr~rrr-rrZ CommandLiner$)rr8rrrrrs zTestProcessWMI.test_cmdlinecCsPtjj|jdd}tj|j}|j\}}}d||f}|j|j|dS)N)r=rz%s\%s) r5r6rAr~rrZGetOwnerr-r)rr8rZdomainrrrrrrs   zTestProcessWMI.test_usernamecCsLtjdtjj|jdd}tj|j}|jj }|j |t |j dS)Ng?)r=r) timesleepr5r6rAr~rrrrr-r*r)rr8rrrrrtest_memory_rss#s    zTestProcessWMI.test_memory_rsscCsjtjdtjj|jdd}tj|j}|jj }t |j }||krf||dkrf|j d||fdS)Ng?)r=rizwmi=%s, psutil=%s) rrr5r6rAr~rrrrr*Z PageFileUsager&)rr8rrZ wmi_usagerrrtest_memory_vms*s    zTestProcessWMI.test_memory_vmscCs\tjj|jdd}tj|j}t|jjdd}t j dt j |j }|j ||dS)N)r=rr\z %Y%m%d%H%M%S)r5r6rAr~rrrZ CreationDaterUrZstrftimeZ localtimerr-)rr8rZ wmic_createZ psutil_createrrrtest_create_time7s  zTestProcessWMI.test_create_timeN)rfrgrh__doc__rrrrrrrrrrrrrrrs   rc@sXeZdZdZeddZeddZddZdd Zd d Z d d Z ddZ ddZ dS)TestDualProcessImplementationa{ Certain APIs on Windows have 2 internal implementations, one based on documented Windows APIs, another one based NtQuerySystemInformation() which gets called as fallback in case the first fails because of limited permission error. Here we test that the two methods return the exact same value, see: https://github.com/giampaolo/psutil/issues/304 cCstj|_dS)N)rr~)rrrrrLsz(TestDualProcessImplementation.setUpClasscCs tdS)N)r)rrrrrPsz+TestDualProcessImplementation.tearDownClassc CsPtj|jj}tjdtjtjd}|j tj|jj|WdQRXdS)Nz psutil._psplatform.cext.proc_exe) side_effect) rrr~rrrdrr+rr-)rrrrrrrWs z'TestDualProcessImplementation.test_namec Cstj|jj}tjdttjddx}tj|jj}|j t |t |xLt t |D]<}|j ||d|j ||d|j ||||ddqZWWdQRXdS)Nz(psutil._psplatform.cext.proc_memory_infomsg)rri)rP)rrr~rrrdrrEPERMr-lenrZassertGreaterEqualrR)rZmem_1rZmem_2rrrrr^sz.TestDualProcessImplementation.test_memory_infoc CsNtj|jj}tjdttjdd}|j tj|jj|WdQRXdS)Nz(psutil._psplatform.cext.proc_create_timer)r) rrr~rrrdrrrr-)rZctimerrrrrjs z.TestDualProcessImplementation.test_create_timec Csntj|jj}tjdttjdd>}tj|jj}|j |j |j dd|j |j |j ddWdQRXdS)Nz&psutil._psplatform.cext.proc_cpu_timesr)rg{Gz?)rP) rrr~Z cpu_timesrrdrrrrRusersystem)rZ cpu_times_1rZ cpu_times_2rrrtest_cpu_timesqsz,TestDualProcessImplementation.test_cpu_timesc Csttj|jj}tjdttjddD}tj|jj}x,t t |D]}|j ||||ddqFWWdQRXdS)Nz(psutil._psplatform.cext.proc_io_countersr)rr0)rP) rrr~rrrdrrrrrrR)rZ io_counters_1rZ io_counters_2rrrrr|sz.TestDualProcessImplementation.test_io_countersc CsNtj|jj}tjdttjdd}|j tj|jj|WdQRXdS)Nz(psutil._psplatform.cext.proc_num_handlesr)r) rrr~rrrdrrrr-)rrrrrrrs z.TestDualProcessImplementation.test_num_handlesN) rfrgrhrrrrrrrrrrrrrrr@s      rc@s|eZdZdZeddZeddZddgZdd Z d d Z ed d Z ddZ ddZ ddZddZddZddZdS)RemoteProcessTestCasezCertain functions require calling ReadProcessMemory. This trivially works when called on the current process. Check that this works on other processes, especially when they have a different bitness. cCsTd}xJtjdD]<}tj|d|gtjtjd}|j\}}|tt kr|SqWdS)Nz6import sys; sys.stdout.write(str(sys.maxsize > 2**32))zC:\Python*\python.exez-c)rstdoutstderr)glob subprocessPopenPIPEZSTDOUT communicater IS_64_BIT)codefilenamer9outputrrrrfind_other_interpreters   z,RemoteProcessTestCase.find_other_interpretercCs@|j}|dkrtjdtr.tj|_||_n||_tj|_dS)Nz0could not find interpreter with opposite bitness)rr ZSkipTestrr executablepython64python32)rZ other_pythonrrrrsz RemoteProcessTestCase.setUpClassz-czimport sys; sys.stdin.read()cCsVtjj}ttj|d<t|jg|j|tj d|_ t|j g|j|tj d|_ dS)NTHINK_OF_A_NUMBER)envstdin) r+r,copyrrrr test_argsrrproc32rproc64)rrrrrsetUps  zRemoteProcessTestCase.setUpcCs|jj|jjtdS)N)rrrr)rrrrtearDowns  zRemoteProcessTestCase.tearDowncCs tdS)N)r)rrrrrsz#RemoteProcessTestCase.tearDownClasscCs@tj|jj}|jt|jd|j|jdd|jdS)Nr]rQ)rrrr~r-rrr)rrrrrtest_cmdline_32sz%RemoteProcessTestCase.test_cmdline_32cCs@tj|jj}|jt|jd|j|jdd|jdS)Nr]rQ)rrrr~r-rrr)rrrrrtest_cmdline_64sz%RemoteProcessTestCase.test_cmdline_64cCs&tj|jj}|j|jtjdS)N)rrrr~r-cwdr+getcwd)rrrrr test_cwd_32sz!RemoteProcessTestCase.test_cwd_32cCs&tj|jj}|j|jtjdS)N)rrrr~r-rr+r)rrrrr test_cwd_64sz!RemoteProcessTestCase.test_cwd_64cCs>tj|jj}|j}|jd||j|dttj dS)Nr) rrrr~r,assertIn assertEqualsrr+r)rrerrrtest_environ_32s z%RemoteProcessTestCase.test_environ_32cCs>tj|jj}|j}|jd||j|dttj dS)Nr) rrrr~r,r r rr+r)rrr rrrtest_environ_64s z%RemoteProcessTestCase.test_environ_64N)rfrgrhr staticmethodrrrrrrrrrrr r rrrrrrs    rc@seZdZddZddZdS) TestServicescCsntdddddddg}tdd d g}tddd d d ddg}x,tjD]}|j}|j|dt|j|djd|j|dt|j|dt|j|d||ddk rtj |d|j|dt|j|dt|j|dt|j|d||j|d||j|dt|j }|dk rLtj |}|j |j tj |j}|j||qFWdS)NZrunningZpausedstartpausecontinuestopZstoppedZ automaticZmanualZdisabledZ start_pendingZ pause_pendingZcontinue_pendingZ stop_pendingrrF display_namerstatusr~ZbinpathZ start_type description)rBrwin_service_iterrZassertIsInstancerZassertNotEqualrr rr~rZZ is_runningwin_service_getrr-)rZvalid_statusesZvalid_start_typesZservdatar~rsrrrtest_win_service_itersR   z"TestServices.test_win_service_iterc'Cspttjj}|jtj}tj|dWdQRX|j|jj|dtj|}t tj j j d}t jd|d|jtj|jWdQRXt jd|d|jtj|jWdQRXt tj j jd}t jd|d|jtj|jWdQRXt jd|d|jtj|jWdQRX|j|jt||j|jt||j|jt||j|jt|dS)Nz???rFz/psutil._psplatform.cext.winservice_query_status)rz/psutil._psplatform.cext.winservice_query_config)nextrrrrrrr-Z exceptionZ WindowsError _psplatformrWZERROR_SERVICE_DOES_NOT_EXISTrrdrrZERROR_ACCESS_DENIEDrr rrrN)rrcmZserviceexcrrrtest_win_service_get!s2     z!TestServices.test_win_service_getN)rfrgrhrr!rrrrrs0r__main__l)2rr^rrr+rrrrrrwarningsrrZpsutil._compatrZ psutil.testsrrrrrr r r r catch_warnings simplefilterr1rrr5 ImportErrorrrrWmaxsizerrriZTestCaser rjr}rrrrrf__file__rrrrsl                   < m A N ^ U