o
    i_                     @   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)which)super)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   T/var/www/edux/Edux_v2/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)self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   )win32api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)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<   w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   rF   rG   )NumberOfCoresrI   r   r   r   rK      s    z:TestCpuAPIs.test_cpu_count_cores_vs_wmi.<locals>.<genexpr>F)logicalrL   )r<   rQ   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)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   )
rM   rN   rP   r9   CurrentClockSpeedr:   cpu_freqcurrentMaxClockSpeedmax)r<   rQ   rJ   r   r   r   test_cpu_freq   s   zTestCpuAPIs.test_cpu_freqN)r   r   r   r   skipIfr7   r8   r>   rE   rS   rW   r\   rc   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)perniczpseudo-interface -z-%r nic wasn't found in 'ipconfig /all' output)r   r:   net_io_counterskeysreplacelowerfail)r<   r1   nicsnicr   r   r   test_nic_names   s   zTestSystemAPIs.test_nic_namesc                 C   s,   t   d }| t|jt j d S r]   )	rM   rN   Win32_ComputerSystemr9   r6   TotalPhysicalMemoryr:   virtual_memorytotalr<   rQ   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)
rM   rN   Win32_PerfRawData_PerfOS_MemoryassertAlmostEqualr6   AvailableBytesr:   rs   freer   ru   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 r]   )rM   rN   ry   r9   r6   CommitLimitr:   rs   rt   swap_memoryr|   usedru   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   _Total)Named   r?   rw   )r:   r   rt   rM   rN   #Win32_PerfRawData_PerfOS_PagingFiler6   PercentUsagePercentUsage_BaseassertGreaterEqualpercentrz   assertLessEqual)r<   rQ   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   	ProcessIdr$   r   r   r   r)      s    z,TestSystemAPIs.test_pids.<locals>.<listcomp>)rM   rN   Win32_Processsetr:   pidsr9   )r<   rQ   wmi_pids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_partitionsrM   rN   Win32_LogicalDiskdevicerk   DeviceID
mountpointopts
startswith
disk_usager   r9   rt   r6   Size	FreeSpacer|   absrm   repr)r<   ps_parts	wmi_partsps_partwmi_partusage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   rw   r"   )r:   r   r   r@   GetDiskFreeSpaceExr   r   rz   r|   r   rt   r9   r   )r<   diskrC   rD   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    
Tr   )r@   GetLogicalDriveStringsr.   r:   r   r9   rB   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)
r   cextnet_if_statsrM   rN   Win32_NetworkAdapteraddr   NetConnectionID
assertTrue)r<   ps_nameswmi_adapters	wmi_names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?   )rM   rN   Win32_OperatingSystemLastBootUpTimer.   datetimestrptimefromtimestampr:   	boot_timer   total_secondsr   )r<   wmi_oswmi_btime_strwmi_btime_dt	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   r[   r   r   r   test_boot_time_fluctuation  s   "z)TestSystemAPIs.test_boot_time_fluctuationN)r   r   r   rp   rv   r}   r   r   r   rd   r   r   r   r   r   r   r   r   r   r   r   r   r   re      s"    




	re   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 )NSystemBatteriesPresent)r@   GetPwrCapabilitiesassertIsNotNoner:   sensors_batteryassertIsNoner[   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"   rw   )rM   rN   queryr:   r   rz   r   EstimatedChargeRemainingr<   rQ   battery_wmi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      )rM   rN   r   r:   r   r9   power_plugged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   secsleft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   POWER_TIME_UNKNOWNr   r   r   r   r   test_emulate_secs_left_unknownW  r   z1TestSensorsBattery.test_emulate_secs_left_unknownN)r   r   r   r   r   rd   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 rG   r   pidclsr   r   r   
setUpClassf     zTestProcess.setUpClassc                 C      t | j d S rG   r   r   r   r   r   r   tearDownClassj  r   zTestProcess.tearDownClassc                 C   s   t d}| t j|j d S r]   )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"   )vistazwin-7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 rG   )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 rG   )r:   r   r   r   r  r  CTRL_C_EVENT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@   GetUserNameExr  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[ ]+rg   r   "r!   r   )resubr@   GetCommandLiner*   r+   r:   r   r   rk   r9   rB   r   r   r   test_cmdline  s
    zTestProcess.test_cmdlinec                 C   sJ   t tjtjt }| t j| t	
|}t  }| || d S rG   )r@   r  r  r  r  r7   r  
addCleanupr  win32processGetPriorityClassr:   r   nicer9   r<   r!  rC   rD   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 )	NPeakWorkingSetSizeWorkingSetSizeQuotaPeakPagedPoolUsageQuotaPagedPoolUsageQuotaPeakNonPagedPoolUsageQuotaNonPagedPoolUsagePagefileUsagePeakPagefileUsage)r@   r  r  r  r  r   r3  r  r4  GetProcessMemoryInfor:   r   r  r9   	peak_wsetwsetpeak_paged_pool
paged_poolpeak_nonpaged_poolnonpaged_poolpagefilepeak_pagefiler  r  r7  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 rG   )r@   r  r  r  r  r   r3  r  r:   r   r   r&  r4  GetExitCodeProcessr9   )r<   r!  r  rD   rC   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>@   )rangerN  r   rN  r   from_bitmask  s   z3TestProcess.test_cpu_affinity.<locals>.from_bitmaskr   )r@   r  r  r  r  r   r3  r  r4  GetProcessAffinityMaskr:   r   cpu_affinityr9   )r<   rQ  r!  rC   rD   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 )NReadOperationCountWriteOperationCountReadTransferCountWriteTransferCountOtherOperationCountOtherTransferCount)r@   r  r  r  r  r7   r  r3  r  r4  GetProcessIoCountersr:   r   io_countersr9   
read_countwrite_count
read_byteswrite_bytesother_countother_bytesr7  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      )ctypesctypes.wintypeswindllkernel32r  r   r3  r  wintypesDWORDGetProcessHandleCountbyrefvaluer:   r   r  r9   )r<   re  r  r!  hndcntrC   rD   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side_effectz
time.sleepr?   )WindowsErrorwinerrorr   r   r:   r   r  r  cwdr   
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:   r   _psplatformr   r  r'  exe)r<   r   rJ   r   r   r   test_exe$  s   zTestProcess.test_exeN)r   r   r   classmethodr   r   r  r  r  r#  r(  r-  r2  r8  rJ  rL  rT  rc  ro  rw  rz  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   rG   r   r   r   r   r   r   /  r   zTestProcessWMI.setUpClassc                 C   r   rG   r   r   r   r   r   r   3  r   zTestProcessWMI.tearDownClassc                 C   s8   t  j| jdd }t| j}| | |j d S Nr   r   )	rM   rN   r   r   r:   r   r9   r
  Captionr<   rQ   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}  )
rM   rN   r   r   r:   r   r9   ry  rl   ExecutablePathr  r   r   r   rz  =  s   zTestProcessWMI.test_exec                 C   sF   t  j| jdd }t| j}| d| |j	
dd d S )Nr   r   rg   r.  r!   )rM   rN   r   r   r:   r   r9   r+   r   CommandLinerk   r  r   r   r   r2  E  s
   zTestProcessWMI.test_cmdlinec                 C   sP   t  j| jdd }t| j}| \}}}d||f }| | | d S )Nr   r   z%s\%s)	rM   rN   r   r   r:   r   GetOwnerr9   r  )r<   rQ   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}  )rM   rN   r   r   r:   r   r  r  r9   r6   r:  )r<   rQ   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 )Nr   r   rd  zwmi=%s, psutil=%s)rM   rN   r   r   r:   r   r  r  r6   PageFileUsagerm   )r<   rQ   r  r  	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 )Nr   r   r   r   )rM   rN   r   r   r:   r   r  CreationDater.   timestrftime	localtimer  r9   )r<   rQ   r  wmic_createpsutil_creater   r   r   test_create_timef  s   zTestProcessWMI.test_create_timeN)r   r   r   __doc__r{  r   r   r  r   rd   r   rz  r2  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   rG   r   r   r   r   r   r   ~  r   z(TestDualProcessImplementation.setUpClassc                 C   r   rG   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msgrp  r   i   rw   )r:   r   r   r  r   r   OSErrorerrnoEPERMr9   rY   rP  r   rz   r   )r<   mem_1funmem_2rM  r   r   r   rJ    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  rp  )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  rp  g{Gz?rw   )r:   r   r   rZ   r   r   r  r  r  r   rz   usersystem)r<   cpu_times_1r  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  rp  r?   rw   )r:   r   r   r\  r   r   r  r  r  rP  rY   rz   r   )r<   io_counters_1r  io_counters_2rM  r   r   r   rc    s   
"z.TestDualProcessImplementation.test_io_countersc                 C   r  )Nz(psutil._psplatform.cext.proc_num_handlesr  rp  )r:   r   r   r  r   r   r  r  r  r9   r   )r<   r  r  r   r   r   ro    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)use_pebF)
r:   r   r   proc_cmdliner  r   
isinstancer  r'  r9   )r<   r   aberrr   r   r   r2    s   z*TestDualProcessImplementation.test_cmdlineN)r   r   r   r  r{  r   r   rJ  r  r  rc  ro  r2  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filenamerJ   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
executablepython64python32r7   r8   copyr  r  r   	test_argsr  r  proc32proc64)r<   other_pythonr  	__class__r   r   r    s.   

zRemoteProcessTestCase.setUpc                    s"   t    | j  | j  d S rG   )r   tearDownr  r  r  r[   r  r   r   r    s   

zRemoteProcessTestCase.tearDownc                 C   @   t | jj}| t| d | | dd  | j d S N   r"   )r:   r   r  r   r9   rY   r   r  r  r   r   r   test_cmdline_32     z%RemoteProcessTestCase.test_cmdline_32c                 C   r  r  )r:   r   r  r   r9   rY   r   r  r  r   r   r   test_cmdline_64  r  z%RemoteProcessTestCase.test_cmdline_64c                 C   &   t | jj}| | t  d S rG   )r:   r   r  r   r9   rt  r7   getcwdr  r   r   r   test_cwd_32     z!RemoteProcessTestCase.test_cwd_32c                 C   r  rG   )r:   r   r  r   r9   rt  r7   r  r  r   r   r   test_cwd_64
  r  z!RemoteProcessTestCase.test_cwd_64c                 C   s>   t | jj}| }| d| | |d tt	  d S )Nr  )
r:   r   r  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 rG   )r:   r   r  r   r8   r  r  r   r   r   test_environ_64  s   z%RemoteProcessTestCase.test_environ_64)r   r   r   r  staticmethodr  r  r  r  r  r  r  r  r  r  __classcell__r   r   r  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)	automaticmanualdisabled)r  r  start_pendingpause_pendingcontinue_pendingstop_pendingr  r
  r!   display_namer  statusr   binpath
start_typedescription)r   r:   win_service_iteras_dictassertIsInstancer  assertNotEqualr*   r  r   r   r   
is_runningwin_service_getr
  r9   )r<   valid_statusesvalid_start_types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_statusrp  z/psutil._psplatform.cext.winservice_query_config)r:   rx  r   ERROR_SERVICE_DOES_NOT_EXISTERROR_ACCESS_DENIEDnextr  r
  r  r'  r  r9   	exceptionr
   rr  r   r   r  r  r  r  r  r  r   )r<   r  r  r
  cmservicer  rv  r   r   r   test_win_service_getT  sR   

z!TestServices.test_win_service_getN)r   r   r   r  r  r   r   r   r   r  !  s    0r  __main__)run_from_name)=r  r   r  r  r7   r  r/  r  r  r  r  r   warningsr:   r   psutil._compatr   r   r   psutil.testsr   r   r   r	   r
   r   r   r   r   r   r   r   r   r   catch_warningssimplefilterr@   r  r4  rM   psutil._pswindowsr   rx  r   rd   r   r    r6   r3   r4   re   r   r   r|  r  r  r  r   psutil.tests.runnerr  __file__r   r   r   r   <module>   s   



' < IFQ\
^