o
    h_                     @   s  d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddlZddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlm Z  ddlm!Z! erese"  e#d ddl$Z$ddl%Z%ddl&Z&ddl'Z'W d   n1 sw   Y  erddl(m)Z) ej*j+Z+e,e de,ede,eoe dG dd deZ-dd Z.e/fddZ0G d d! d!e-Z1G d"d# d#e-Z2G d$d% d%e-Z3G d&d' d'e-Z4G d(d) d)e-Z5e,e dG d*d+ d+eZ6e,e dG d,d- d-eZ7e,e dG d.d/ d/eZ8e9d0kridd1l:m;Z; e;e< dS dS )2zWindows specific tests.    N)WINDOWS)FileNotFoundError)super)which)APPVEYOR)GITHUB_ACTIONS)HAS_BATTERY)IS_64BIT)PY3)PYPY)TOLERANCE_DISK_USAGE)TOLERANCE_SYS_MEM)PsutilTestCase)mock)retry_on_failure)sh)spawn_testproc)	terminateignore)convert_oserrorzWINDOWS onlyzpywin32 not available on PYPYzpywin32 broken on GITHUB + PY2c                   @   s   e Zd ZdS )WindowsTestCaseN)__name__
__module____qualname__ r   r   p/Users/merlin/projects/employee-monitoring-system/venv/lib/python3.10/site-packages/psutil/tests/test_windows.pyr   ;   s    r   c                 C   s&   t ds	tddd|   }t|S )zCurrently not used, but avalable just in case. Usage:

    >>> powershell(
        "Get-CIMInstance Win32_PageFileUsage | Select AllocatedBaseSize")
    zpowershell.exezpowershell.exe not availablez?powershell.exe -ExecutionPolicy Bypass -NoLogo -NonInteractive z,-NoProfile -WindowStyle Hidden -Command "%s")r   unittestSkipTestr   )cmdcmdliner   r   r   
powershellC   s   
r    c                    sd   t d| |f  }d| dd  } dur0d|v r,t fdd| D S  |S |S )zCurrently not used, but avalable just in case. Usage:

    >>> wmic("Win32_OperatingSystem", "FreePhysicalMemory")
    2134124534
    zwmic path %s get %s    N,c                    s   g | ]} |qS r   r   .0x	converterr   r   
<listcomp>[   s    zwmic.<locals>.<listcomp>)r   stripjoin
splitlinestuplesplit)pathwhatr(   outdatar   r'   r   wmicQ   s   r3   c                   @   sN   e Zd Zedejvddd Zdd Zdd Z	d	d
 Z
dd Zdd ZdS )TestCpuAPIsNUMBER_OF_PROCESSORSz-NUMBER_OF_PROCESSORS env var is not availablec                 C   s"   t tjd }| |t  d S )Nr5   )intosenvironassertEqualpsutil	cpu_count)selfZnum_cpusr   r   r   &test_cpu_count_vs_NUMBER_OF_PROCESSORSi   s   z2TestCpuAPIs.test_cpu_count_vs_NUMBER_OF_PROCESSORSc                 C   s$   t  d }t }| || d S )N   )win32apiZGetSystemInfor:   r;   r9   r<   	sys_valuepsutil_valuer   r   r   test_cpu_count_vs_GetSystemInfoq   s   z+TestCpuAPIs.test_cpu_count_vs_GetSystemInfoc                 C   s2   t  }tdd | D }| t | d S )Nc                 s       | ]}|j V  qd S N)ZNumberOfLogicalProcessorsr%   procr   r   r   	<genexpr>z   s    z<TestCpuAPIs.test_cpu_count_logical_vs_wmi.<locals>.<genexpr>wmiWMIsumWin32_Processorr9   r:   r;   )r<   wZprocsr   r   r   test_cpu_count_logical_vs_wmix   s
   z)TestCpuAPIs.test_cpu_count_logical_vs_wmic                 C   s6   t  }tdd | D }| tjdd| d S )Nc                 s   rD   rE   )ZNumberOfCoresrF   r   r   r   rH      s    z:TestCpuAPIs.test_cpu_count_cores_vs_wmi.<locals>.<genexpr>F)ZlogicalrI   )r<   rN   Zcoresr   r   r   test_cpu_count_cores_vs_wmi~   s   z'TestCpuAPIs.test_cpu_count_cores_vs_wmic                 C   s    |  t ttjdd d S )NT)Zpercpu)r9   r:   r;   len	cpu_timesr<   r   r   r   test_cpu_count_vs_cpu_times   s   
z'TestCpuAPIs.test_cpu_count_vs_cpu_timesc                 C   s@   t  }| d }| |jt j | |jt j	 d S Nr   )
rJ   rK   rM   r9   ZCurrentClockSpeedr:   Zcpu_freqcurrentZMaxClockSpeedmax)r<   rN   rG   r   r   r   test_cpu_freq   s   zTestCpuAPIs.test_cpu_freqN)r   r   r   r   skipIfr7   r8   r=   rC   rO   rP   rT   rX   r   r   r   r   r4   g   s    
r4   c                   @   s   e Zd Zdd Zdd Zdd Zdd Zd	d
 Ze	e
de dd Ze dd Ze dd Zdd Zdd Zdd Zdd ZdS )TestSystemAPIsc                 C   sR   t d}tjdd }|D ]}d|dd v rq||vr&| d| qd S )Nzipconfig /allT)Zperniczpseudo-interface -z-%r nic wasn't found in 'ipconfig /all' output)r   r:   Znet_io_counterskeysreplacelowerfail)r<   r1   ZnicsZnicr   r   r   test_nic_names   s   zTestSystemAPIs.test_nic_namesc                 C   s,   t   d }| t|jt j d S rU   )	rJ   rK   ZWin32_ComputerSystemr9   r6   ZTotalPhysicalMemoryr:   virtual_memorytotalr<   rN   r   r   r   test_total_phymem   s   z TestSystemAPIs.test_total_phymemc                 C   s0   t   d }| jt|jt jt	d d S )Nr   delta)
rJ   rK   Win32_PerfRawData_PerfOS_MemoryassertAlmostEqualr6   ZAvailableBytesr:   rb   freer   rd   r   r   r   test_free_phymem   s
   
zTestSystemAPIs.test_free_phymemc                 C   sl   t   d }| t|jt j t	 j t	 jdkr4| dt	 j
 | dt	 j d S d S rU   )rJ   rK   rh   r9   r6   ZCommitLimitr:   rb   rc   swap_memoryrj   usedrd   r   r   r   test_total_swapmem   s   z!TestSystemAPIs.test_total_swapmemc                 C   s|   t  jdkr<t jddd }t|jd t|j }| 	t  j
d | jt  j
|dd | t  j
d d S d S )Nr   Z_Total)Named   r>   rf   )r:   rl   rc   rJ   rK   Z#Win32_PerfRawData_PerfOS_PagingFiler6   ZPercentUsageZPercentUsage_BaseassertGreaterEqualpercentri   assertLessEqual)r<   rN   ZpercentSwapr   r   r   test_percent_swapmem   s   z#TestSystemAPIs.test_percent_swapmemztest not relieable on appveyorc                 C   s:   t   }tdd |D }tt }| || d S )Nc                 S   s   g | ]}|j qS r   Z	ProcessIdr$   r   r   r   r)      s    z,TestSystemAPIs.test_pids.<locals>.<listcomp>)rJ   rK   Win32_Processsetr:   pidsr9   )r<   rN   Zwmi_pidsZpsutil_pidsr   r   r   	test_pids   s   zTestSystemAPIs.test_pidsc              
   C   s   t jdd}t  }|D ]l}|D ]^}|jdd|jkrp|js# nWd|j	v r* nP|j
dr2 nHzt |j}W n
 tyD   Y  n6w | |jt|j t|j}| |j| t|j| dkrn| d|j|f  n
q| d	t| qd S )
NTall\r!   cdromA:i   zpsutil=%s, wmi=%szcan't find partition %s)r:   disk_partitionsrJ   rK   ZWin32_LogicalDiskZdevicer^   ZDeviceID
mountpointopts
startswith
disk_usager   r9   rc   r6   SizeZ	FreeSpacerj   absr`   repr)r<   Zps_partsZ	wmi_partsZps_partZwmi_partusageZwmi_freer   r   r   
test_disks   s8   

zTestSystemAPIs.test_disksc                 C   sx   t  D ]5}d|jv rqt|j}t |j}| j|d |jt	d | j|d |j
t	d | |j|j
|j  qd S )Nr}   r   rf   r"   )r:   r   r   r?   ZGetDiskFreeSpaceExr   r   ri   rj   r   rc   r9   rm   )r<   ZdiskrA   rB   r   r   r   test_disk_usage   s   

zTestSystemAPIs.test_disk_usagec                 C   s>   dd t  dD }dd tjddD }| || d S )Nc                 S   s"   g | ]}|r| d s|d qS )r~   r|   )r   r$   r   r   r   r)      s    z7TestSystemAPIs.test_disk_partitions.<locals>.<listcomp>z\ c                 S   s   g | ]}|j d s|j qS )r~   )r   r   r$   r   r   r   r)      s    
Trz   )r?   ZGetLogicalDriveStringsr.   r:   r   r9   r@   r   r   r   test_disk_partitions   s
   z#TestSystemAPIs.test_disk_partitionsc                 C   s\   t t }t  }t  }|D ]}||j ||j q| 	||@ d||f  d S )Nzno common entries in %s, %s)
rw   cextZnet_if_statsrJ   rK   ZWin32_NetworkAdapteraddro   ZNetConnectionID
assertTrue)r<   Zps_namesZwmi_adaptersZ	wmi_namesZwmi_adapterr   r   r   test_net_if_stats  s   

z TestSystemAPIs.test_net_if_statsc                 C   s^   t   }|d jdd }tj|d}tjt	 }t
||  }| |d d S )Nr   .%Y%m%d%H%M%Sr>   )rJ   rK   ZWin32_OperatingSystemZLastBootUpTimer.   datetimestrptimefromtimestampr:   	boot_timer   total_secondsrs   )r<   Zwmi_osZwmi_btime_strZwmi_btime_dtZ	psutil_dtdiffr   r   r   test_boot_time  s   zTestSystemAPIs.test_boot_timec                 C   s   t jddd | t d W d    n1 sw   Y  t jddd | t d W d    n1 s9w   Y  t jddd | t d W d    n1 sXw   Y  t jddd | t d W d    d S 1 sxw   Y  d S )Nz psutil._pswindows.cext.boot_timer>   return_value      iM  )r   patchr9   r:   r   rS   r   r   r   test_boot_time_fluctuation  s   "z)TestSystemAPIs.test_boot_time_fluctuationN)r   r   r   ra   re   rk   rn   rt   r   rY   r   r   ry   r   r   r   r   r   r   r   r   r   r   rZ      s"    




	rZ   c                   @   s`   e Zd Zdd Zee ddd Zee ddd Zdd	 Z	d
d Z
dd Zdd ZdS )TestSensorsBatteryc                 C   s0   t  d r| t  d S | t  d S )NZSystemBatteriesPresent)r?   ZGetPwrCapabilitiesZassertIsNotNoner:   sensors_batteryassertIsNonerS   r   r   r   test_has_battery*  s   z#TestSensorsBattery.test_has_batteryz
no batteryc                 C   s6   t  }|dd }t }| j|j|jdd d S )Nselect * from Win32_Batteryr   r"   rf   )rJ   rK   queryr:   r   ri   rr   ZEstimatedChargeRemainingr<   rN   Zbattery_wmiZbattery_psutilr   r   r   test_percent0  s   
zTestSensorsBattery.test_percentc                 C   s6   t  }|dd }t }| |j|jdk d S )Nr   r      )rJ   rK   r   r:   r   r9   Zpower_pluggedZBatteryStatusr   r   r   r   test_power_plugged9  s   z%TestSensorsBattery.test_power_pluggedc                 C   sL   t jddd}| t  |jsJ W d    d S 1 sw   Y  d S )N&psutil._pswindows.cext.sensors_battery)r      r   r   r   )r   r   r   r:   r   calledr<   mr   r   r   test_emulate_no_batteryC  s   "z*TestSensorsBattery.test_emulate_no_batteryc                 C   R   t jddd}| t jtj |jsJ W d    d S 1 s"w   Y  d S )Nr   )r"   r   r   r   r   r   r   r9   r:   r   secsleftZPOWER_TIME_UNLIMITEDr   r   r   r   r   test_emulate_power_connectedI     "z/TestSensorsBattery.test_emulate_power_connectedc                 C   r   )Nr   )r      r   r   r   r   r   r   r   r   test_emulate_power_chargingP  r   z.TestSensorsBattery.test_emulate_power_chargingc                 C   r   )Nr   )r   r   r   r   )r   r   r9   r:   r   r   ZPOWER_TIME_UNKNOWNr   r   r   r   r   test_emulate_secs_left_unknownW  r   z1TestSensorsBattery.test_emulate_secs_left_unknownN)r   r   r   r   r   rY   r   r   r   r   r   r   r   r   r   r   r   r   (  s    

	r   c                   @   s   e Zd Zedd Zedd Zdd Zdd Zd	d
 Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#S )$TestProcessc                 C      t  j| _d S rE   r   pidclsr   r   r   
setUpClassf     zTestProcess.setUpClassc                 C      t | j d S rE   r   r   r   r   r   r   tearDownClassj  r   zTestProcess.tearDownClassc                 C   s   t d}| t j|j d S rU   )r:   ProcessassertRaisesAccessDeniedkillr<   pr   r   r   test_issue_24n  s   
zTestProcess.test_issue_24c                 C   s   t d}| | d t| |  | | dk z| d d \}}W n t j	y>   t
 d dvr; Y d S w | |dk d S )Nr   Systemg        r   r"   )Zvistazwin-7Zwin7r   )r:   r   r9   namestrusernamer   create_timememory_infor   platformuname)r<   r   rssvmsr   r   r   test_special_pidr  s   
zTestProcess.test_special_pidc                 C   s"   t | j}| t|jtj d S rE   )r:   r   r   r   
ValueErrorsend_signalsignalSIGINTr   r   r   r   test_send_signal  s   zTestProcess.test_send_signalc                 C   sb   t t }| }ttjtj	t }| }| 
||d  t| | 
| | d S )Nr"   )r:   r   r7   getpidnum_handlesr?   OpenProcesswin32conPROCESS_QUERY_INFORMATIONFALSEr9   CloseHandle)r<   r   beforehandleafterr   r   r   test_num_handles_increment  s   

z&TestProcess.test_num_handles_incrementc                 C   sd   t |  j}|tj |tj |  |	  | 
t j|jtj | 
t j|jtj d S rE   )r:   r   r   r   r   r   ZCTRL_C_EVENTZCTRL_BREAK_EVENTr   waitr   NoSuchProcessr   r   r   r   test_ctrl_signals  s   zTestProcess.test_ctrl_signalsc                 C   s8   t tj}|drtd| t	 
 | d S )N$zrunning as service account)r?   ZGetUserNameExr   ZNameSamCompatibleendswithr   r   r9   r:   r   r   )r<   r   r   r   r   test_username  s   

zTestProcess.test_usernamec                 C   sf   t ddt  }dt  }|d d  kr"|d kr+n n|	ddd}| 
|| d S )Nz[ ]+r[   r   "r!   r   )resubr?   ZGetCommandLiner*   r+   r:   r   r   r^   r9   r@   r   r   r   test_cmdline  s
    zTestProcess.test_cmdlinec                 C   sJ   t tjtjt }| t j| t	
|}t  }| || d S rE   )r?   r   r   r   r   r7   r   
addCleanupr   win32processZGetPriorityClassr:   r   nicer9   r<   r   rA   rB   r   r   r   	test_nice  s   

zTestProcess.test_nicec                 C   s   t tjtj| j}| t j| t	|}t
| j }| |d |j | |d |j | |d |j | |d |j | |d |j | |d |j | |d |j | |d |j | |j|j | |j|j d S )	NZPeakWorkingSetSizeWorkingSetSizeZQuotaPeakPagedPoolUsageZQuotaPagedPoolUsageZQuotaPeakNonPagedPoolUsageZQuotaNonPagedPoolUsageZPagefileUsageZPeakPagefileUsage)r?   r   r   r   r   r   r   r   r   ZGetProcessMemoryInfor:   r   r   r9   Z	peak_wsetZwsetZpeak_paged_poolZ
paged_poolZpeak_nonpaged_poolZnonpaged_poolZpagefileZpeak_pagefiler   r   r   r   r   r   test_memory_info  sB   







zTestProcess.test_memory_infoc                 C   sX   t tjtj| j}| t j| t	| j}|
  | }t|}| || d S rE   )r?   r   r   r   r   r   r   r   r:   r   r   r   r   ZGetExitCodeProcessr9   )r<   r   r   rB   rA   r   r   r   	test_wait  s   
zTestProcess.test_waitc                 C   s\   dd }t tjtj| j}| t j| |t	|d }t
| j }| || d S )Nc                    s    fddt dD S )Nc                    s   g | ]
}d |>  @ r|qS )r"   r   )r%   ir&   r   r   r)     s    zGTestProcess.test_cpu_affinity.<locals>.from_bitmask.<locals>.<listcomp>@   )ranger   r   r   r   from_bitmask  s   z3TestProcess.test_cpu_affinity.<locals>.from_bitmaskr   )r?   r   r   r   r   r   r   r   r   ZGetProcessAffinityMaskr:   r   Zcpu_affinityr9   )r<   r   r   rA   rB   r   r   r   test_cpu_affinity  s   zTestProcess.test_cpu_affinityc                 C   s   t tjtjt }| t j| t	
|}t  }| |j|d  | |j|d  | |j|d  | |j|d  | |j|d  | |j|d  d S )NZReadOperationCountZWriteOperationCountZReadTransferCountZWriteTransferCountZOtherOperationCountZOtherTransferCount)r?   r   r   r   r   r7   r   r   r   r   ZGetProcessIoCountersr:   r   io_countersr9   Z
read_countZwrite_count
read_byteswrite_bytesother_countZother_bytesr   r   r   r   test_io_counters  s0   







zTestProcess.test_io_countersc                 C   s   dd l }dd l}d}|jj|d| j}| |jjj| |j	 }|jj
||| |j}t| j }| || d S )Nr      )ctypesZctypes.wintypesZwindllZkernel32r   r   r   r   ZwintypesZDWORDZGetProcessHandleCountZbyrefvaluer:   r   r   r9   )r<   r  r   r   ZhndcntrA   rB   r   r   r   test_num_handles  s   

zTestProcess.test_num_handlesc              	   C   s   t  }d|_tjd|d) td}t }| tj|j W d    n1 s*w   Y  W d    n1 s9w   Y  | 	|j
d d S )Ni+  z psutil._psplatform.cext.proc_cwdZside_effectz
time.sleepr>   )WindowsErrorwinerrorr   r   r:   r   r   r   cwdrq   Z
call_count)r<   excr   r   r   r   r   test_error_partial_copy  s   z#TestProcess.test_error_partial_copyc                 C   s0   t  d d }t j|}| t j|j d S )Nr   i )r:   rx   _psplatformr   r   r   exe)r<   r   rG   r   r   r   test_exe$  s   zTestProcess.test_exeN)r   r   r   classmethodr   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r
  r  r   r   r   r   r   d  s(    


	

r   c                   @   sx   e Zd ZdZedd Zedd Zdd Ze	e
dd	d
 Zdd Zdd Ze dd Ze dd Zdd ZdS )TestProcessWMIz%Compare Process API results with WMI.c                 C   r   rE   r   r   r   r   r   r   /  r   zTestProcessWMI.setUpClassc                 C   r   rE   r   r   r   r   r   r   3  r   zTestProcessWMI.tearDownClassc                 C   s8   t  j| jdd }t| j}| | |j d S Nru   r   )	rJ   rK   rv   r   r:   r   r9   r   ZCaptionr<   rN   r   r   r   r   	test_name7  s   zTestProcessWMI.test_namez!unreliable path on GITHUB_ACTIONSc                 C   s@   t  j| jdd }t| j}| |  |j	  d S r  )
rJ   rK   rv   r   r:   r   r9   r  r_   ZExecutablePathr  r   r   r   r  =  s   zTestProcessWMI.test_exec                 C   sF   t  j| jdd }t| j}| d| |j	
dd d S )Nru   r   r[   r   r!   )rJ   rK   rv   r   r:   r   r9   r+   r   ZCommandLiner^   r  r   r   r   r   E  s
   zTestProcessWMI.test_cmdlinec                 C   sP   t  j| jdd }t| j}| \}}}d||f }| | | d S )Nru   r   z%s\%s)	rJ   rK   rv   r   r:   r   ZGetOwnerr9   r   )r<   rN   r   domain_r   r   r   r   r   K  s
   zTestProcessWMI.test_usernamec                 C   sB   t  j| jdd }t| j}| j}| |t	|j
 d S r  )rJ   rK   rv   r   r:   r   r   r   r9   r6   r   )r<   rN   r   r   r   r   r   test_memory_rssR  s   
zTestProcessWMI.test_memory_rssc                 C   sd   t  j| jdd }t| j}| j}t|j	}||kr.||d kr0| 
d||f d S d S )Nru   r   r  zwmi=%s, psutil=%s)rJ   rK   rv   r   r:   r   r   r   r6   ZPageFileUsager`   )r<   rN   r   r   Z	wmi_usager   r   r   test_memory_vmsY  s   

zTestProcessWMI.test_memory_vmsc                 C   s\   t  j| jdd }t| j}t|jdd }t	
dt	| }| || d S )Nru   r   r   r   )rJ   rK   rv   r   r:   r   r   ZCreationDater.   timestrftime	localtimer   r9   )r<   rN   r   Zwmic_createZpsutil_creater   r   r   test_create_timef  s   zTestProcessWMI.test_create_timeN)r   r   r   __doc__r  r   r   r  r   rY   r   r  r   r   r   r  r  r  r   r   r   r   r  ,  s     





r  c                   @   sX   e Zd ZdZedd Zedd Zdd Zdd	 Zd
d Z	dd Z
dd Zdd ZdS )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
    c                 C   r   rE   r   r   r   r   r   r   ~  r   z(TestDualProcessImplementation.setUpClassc                 C   r   rE   r   r   r   r   r   r     r   z+TestDualProcessImplementation.tearDownClassc                 C   s   t | j }tjdttjddE}t | j }| 	t
|t
| tt
|D ]}| || d | || d | j|| || dd q,|jsPJ W d    d S 1 s[w   Y  d S )Nz(psutil._psplatform.cext.proc_memory_infomsgr  r   i   rf   )r:   r   r   r   r   r   OSErrorerrnoEPERMr9   rQ   r   rq   ri   r   )r<   Zmem_1funZmem_2r   r   r   r   r     s   
"z.TestDualProcessImplementation.test_memory_infoc                 C   n   t | j }tjdttjdd}| 	t | j | |j
s%J W d    d S 1 s0w   Y  d S )N"psutil._psplatform.cext.proc_timesr  r  )r:   r   r   r   r   r   r  r  r   r9   r   )r<   ctimer!  r   r   r   r    s   
"z.TestDualProcessImplementation.test_create_timec                 C   s   t | j }tjdttjdd*}t | j }|j	s!J | j
|j|jdd | j
|j|jdd W d    d S 1 s@w   Y  d S )Nr#  r  r  g{Gz?rf   )r:   r   r   rR   r   r   r  r  r   r   ri   usersystem)r<   Zcpu_times_1r!  Zcpu_times_2r   r   r   test_cpu_times  s   



"z,TestDualProcessImplementation.test_cpu_timesc                 C   s   t | j }tjdttjdd+}t | j }t	t
|D ]}| j|| || dd q"|js6J W d    d S 1 sAw   Y  d S )Nz(psutil._psplatform.cext.proc_io_countersr  r  r>   rf   )r:   r   r   r   r   r   r  r  r   r   rQ   ri   r   )r<   Zio_counters_1r!  Zio_counters_2r   r   r   r   r     s   
"z.TestDualProcessImplementation.test_io_countersc                 C   r"  )Nz(psutil._psplatform.cext.proc_num_handlesr  r  )r:   r   r   r   r   r   r  r  r   r9   r   )r<   r   r!  r   r   r   r    s   
"z.TestDualProcessImplementation.test_num_handlesc                 C   s   t  D ]9}ztj|dd}tj|dd}W n  ty6 } zt|}t|t jt jfs, W Y d }~qd }~ww | 	|| qd S )NT)Zuse_pebF)
r:   rx   r   Zproc_cmdliner  r   
isinstancer   r   r9   )r<   r   aberrr   r   r   r     s   z*TestDualProcessImplementation.test_cmdlineN)r   r   r   r  r  r   r   r   r  r'  r   r  r   r   r   r   r   r  r  s    



r  c                       sp   e Zd ZdZedd ZddgZ fddZ fdd	Zd
d Z	dd Z
dd Zdd Zdd Zdd Z  ZS )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.
    c                  C   s\   d} t  dD ]$}tj|d| gtjtjd}| \}}|  |tt kr+|  S qd S )Nz6import sys; sys.stdout.write(str(sys.maxsize > 2**32))zC:\Python*\python.exe-c)argsstdoutstderr)	glob
subprocessPopenPIPESTDOUTcommunicater   r   r	   )codefilenamerG   outputr  r   r   r   find_other_interpreter  s   z,RemoteProcessTestCase.find_other_interpreterr-  zimport sys; sys.stdin.read()c                    s   t    |  }|d u rtdtrtj| _|| _	n|| _tj| _	t
j }tt
 |d< | j| j	g| j |tjd| _| j| jg| j |tjd| _d S )Nz0could not find interpreter with opposite bitnessTHINK_OF_A_NUMBER)envstdin)r   setUpr:  r   r   r	   sys
executableZpython64Zpython32r7   r8   copyr   r   r   	test_argsr2  r4  proc32proc64)r<   Zother_pythonr<  	__class__r   r   r>    s.   

zRemoteProcessTestCase.setUpc                    s"   t    | j  | j  d S rE   )r   tearDownrC  r6  rD  rS   rE  r   r   rG    s   

zRemoteProcessTestCase.tearDownc                 C   @   t | jj}| t| d | | dd  | j d S N   r"   )r:   r   rC  r   r9   rQ   r   rB  r   r   r   r   test_cmdline_32     z%RemoteProcessTestCase.test_cmdline_32c                 C   rH  rI  )r:   r   rD  r   r9   rQ   r   rB  r   r   r   r   test_cmdline_64  rL  z%RemoteProcessTestCase.test_cmdline_64c                 C   &   t | jj}| | t  d S rE   )r:   r   rC  r   r9   r  r7   getcwdr   r   r   r   test_cwd_32     z!RemoteProcessTestCase.test_cwd_32c                 C   rN  rE   )r:   r   rD  r   r9   r  r7   rO  r   r   r   r   test_cwd_64
  rQ  z!RemoteProcessTestCase.test_cwd_64c                 C   s>   t | jj}| }| d| | |d tt	  d S )Nr;  )
r:   r   rC  r   r8   assertInr9   r   r7   r   )r<   r   er   r   r   test_environ_32  s   z%RemoteProcessTestCase.test_environ_32c                 C   s4   t | jj}z|  W d S  t jy   Y d S w rE   )r:   r   rD  r   r8   r   r   r   r   r   test_environ_64  s   z%RemoteProcessTestCase.test_environ_64)r   r   r   r  staticmethodr:  rB  r>  rG  rK  rM  rP  rR  rU  rV  __classcell__r   r   rE  r   r,    s    
r,  c                   @   s   e Zd Zdd Zdd ZdS )TestServicesc                 C   sN  t g d}t g d}t g d}t D ]}| }| |d t | |d  d | |d t | |d t | |d | |d	 d urSt	|d	  | |d
 t | |d t | |d t | |d | | |d | | |d t |
 }|d urt	|}| |  t| }| || qd S )N)runningpausedstartpausecontinuestopstopped)Z	automaticZmanualdisabled)rZ  r[  Zstart_pendingZpause_pendingZcontinue_pendingZstop_pendingr`  r   r!   display_namer   statusr   ZbinpathZ
start_typedescription)rw   r:   win_service_iteras_dictZassertIsInstancer   ZassertNotEqualr*   rS  r   r   r   
is_runningwin_service_getr   r9   )r<   Zvalid_statusesZvalid_start_typesZservr2   r   r   sr   r   r   test_win_service_iter$  s2   		
z"TestServices.test_win_service_iterc                 C   s  t jjj}t jjj}tt   }| t j	}t 
|d  W d    n1 s*w   Y  | |jj|d  t 
|}trGddd|f}n|df}t| }tjd|d | t j	|j W d    n1 siw   Y  tjd|d | t j	|j W d    n1 sw   Y  trddd|f}n|df}t| }tjd|d | t j|j W d    n1 sw   Y  tjd|d | t j|j W d    n1 sw   Y  | | t| | | t| | | t| | | t| d S )Nz???r   r  z/psutil._psplatform.cext.winservice_query_statusr  z/psutil._psplatform.cext.winservice_query_config)r:   r  r   ERROR_SERVICE_DOES_NOT_EXISTERROR_ACCESS_DENIEDnextre  r   r   r   rh  r9   	exceptionr
   r  r   r   rc  r   r   rS  r   rb  r   )r<   rk  rl  r   cmZservicer.  r	  r   r   r   test_win_service_getT  sR   

z!TestServices.test_win_service_getN)r   r   r   rj  rp  r   r   r   r   rY  !  s    0rY  __main__)run_from_name)=r  r   r  r1  r7   r   r   r   r2  r?  r  r   warningsr:   r   Zpsutil._compatr   r   r   Zpsutil.testsr   r   r   r	   r
   r   r   r   r   r   r   r   r   r   catch_warningssimplefilterr?   r   r   rJ   Zpsutil._pswindowsr   r  r   rY   r   r    r6   r3   r4   rZ   r   r   r  r  r,  rY  r   Zpsutil.tests.runnerrr  __file__r   r   r   r   <module>   s   



' < IFQ\
^