<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.3.0/css/all.min.css"
        integrity="sha512-SzlrxWUlpfuzQ+pcUCosxcglQRNAq/DZjVsC0lE40xsADsfeQoEypE+enwcOiGjk/bSuGGKHEyjSoQ1zVisanQ=="
        crossorigin="anonymous" referrerpolicy="no-referrer" />
</head>
</html>
3
JZK                 @   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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j Z ejd y$ddlZddlZddlZddlZW n$ e k
rD   ej!dkr@ Y nX W dQ R X ej"j#Z#e	j$d$kZ%dd Z&ej'e dG dd dej(Z)ej'e dG dd dej(Z*ej'e dG dd dej(Z+ej'e dG dd dej(Z,ej'e dG dd dej(Z-ej'e dG dd  d ej(Z.ej'e dG d!d" d"ej(Z/e0d#kr\ee1 dS )%zWindows specific tests.    N)WINDOWS)callable)APPVEYOR)get_test_subprocess)HAS_BATTERY)mock)reap_children)retry_before_failing)run_test_module_by_name)sh)unittestignorent       c                s    fdd}|S )Nc                st   y | f||S  t k
rn } zBddlm} |j|krDtjd d |jtjkr\tjd d  W Y d d }~X nX d S )Nr   )ACCESS_DENIED_SET)OSErrorZpsutil._pswindowsr   errnopsutilAccessDeniedZESRCHNoSuchProcess)selfargskwargserrr   )fun $/usr/lib64/python3.6/test_windows.pywrapper5   s    
z wrap_exceptions.<locals>.wrapperr   )r   r   r   )r   r   wrap_exceptions4   s    
r   zWINDOWS onlyc               @   s   e Zd Zdd Zejdejkddd Zdd Z	d	d
 Z
dd Zejede dd Ze dd Zdd Zdd Zdd Zdd Zdd ZdS )TestSystemAPIsc             C   sV   t d}tjddj }x8|D ]0}d|jddj kr8q||kr| jd|  qW 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   outZnicsZnicr   r   r   test_nic_namesJ   s    
zTestSystemAPIs.test_nic_namesNUMBER_OF_PROCESSORSz-NUMBER_OF_PROCESSORS env var is not availablec             C   s"   t tjd }| j|tj  d S )Nr)   )intosenvironassertEqualr   	cpu_count)r   Znum_cpusr   r   r   test_cpu_countT   s    zTestSystemAPIs.test_cpu_countc             C   s$   t j d }tj }| j|| d S )N   )win32apiZGetSystemInfor   r.   r-   )r   	sys_valuepsutil_valuer   r   r   test_cpu_count_2Z   s    zTestSystemAPIs.test_cpu_count_2c             C   s@   t j }|j d }| j|jtj j | j|jtj j	 d S )Nr   )
wmiWMIZWin32_Processorr-   ZCurrentClockSpeedr   Zcpu_freqZcurrentZMaxClockSpeedmax)r   wprocr   r   r   test_cpu_freq_   s    zTestSystemAPIs.test_cpu_freqc             C   s,   t j j d }| jt|jtj j d S )Nr   )	r5   r6   ZWin32_ComputerSystemr-   r*   ZTotalPhysicalMemoryr   Zvirtual_memorytotal)r   r8   r   r   r   test_total_phymeme   s    z TestSystemAPIs.test_total_phymemztest not relieable on appveyorc             C   s:   t j j }tdd |D }ttj }| j|| d S )Nc             S   s   g | ]
}|j qS r   )	ProcessId).0xr   r   r   
<listcomp>|   s    z,TestSystemAPIs.test_pids.<locals>.<listcomp>)r5   r6   Win32_Processsetr   Zpidsr-   )r   r8   Zwmi_pidsZpsutil_pidsr   r   r   	test_pidsv   s    zTestSystemAPIs.test_pidsc             C   s   t jdd}tj j }x|D ]}x|D ]}|jjdd|jkr(|jsHP yt j	|j}W n4 t
k
r } z|jtjkrzP n W Y d d }~X nX | j|jt|j t|j}| j|j| t|j| d
kr| jd|j|f  P q(W | jdt|  qW d S )NT)all\ 
   i   zpsutil=%s, wmi=%szcan't find partition %si (  i   )r   disk_partitionsr5   r6   ZWin32_LogicalDiskZdevicer$   ZDeviceID
mountpoint
disk_usager   r   ENOENTr-   r;   r*   ZSizeZ	FreeSpacefreeabsr&   repr)r   Zps_partsZ	wmi_partsZps_partZwmi_partZusager   Zwmi_freer   r   r   
test_disks   s*    


zTestSystemAPIs.test_disksc             C   sp   xjt j D ]^}tj|j}t j|j}| j|d |jdd | j|d |jdd | j	|j
|j|j  q
W d S )Nr   i   )delta   i   i   )r   rH   r1   ZGetDiskFreeSpaceExrI   rJ   assertAlmostEqualrL   r;   r-   Zused)r   Zdiskr2   r3   r   r   r   test_disk_usage   s    zTestSystemAPIs.test_disk_usagec             C   s>   dd t j jdD }dd tjddD }| j|| d S )Nc             S   s$   g | ]}|r|j d  r|d qS )zA:rE   )
startswith)r>   r?   r   r   r   r@      s    z7TestSystemAPIs.test_disk_partitions.<locals>.<listcomp>z\ c             S   s   g | ]
}|j qS r   )rI   )r>   r?   r   r   r   r@      s    T)rD   )r1   ZGetLogicalDriveStringssplitr   rH   r-   )r   r2   r3   r   r   r   test_disk_partitions   s    z#TestSystemAPIs.test_disk_partitionsc             C   s`   t tj }tj j }t  }x$|D ]}|j|j |j|j q$W | j	||@ d||f  d S )Nzno common entries in %s, %s)
rB   cextZnet_if_statsr5   r6   ZWin32_NetworkAdapteraddNameZ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 j j }|d jjdd }tjj|d}tjjtj	 }t
|| j }| j|d d S )Nr   .z%Y%m%d%H%M%S   )r5   r6   ZWin32_OperatingSystemZLastBootUpTimerU   datetimeZstrptimeZfromtimestampr   	boot_timerM   Ztotal_secondsZassertLessEqual)r   Zwmi_osZwmi_btime_strZwmi_btime_dtZ	psutil_dtZdiffr   r   r   test_boot_time   s    zTestSystemAPIs.test_boot_timec             C   s   t jddd | jtj d W d Q R X t jddd | jtj d W d Q R X t jddd | jtj d W d Q R X t jddd | jtj d W d Q R X d S )Nz psutil._pswindows.cext.boot_timer0   )return_value      iM  )r   patchr-   r   r_   )r   r   r   r   test_boot_time_fluctuation   s    z)TestSystemAPIs.test_boot_time_fluctuationN)__name__
__module____qualname__r(   r   skipIfr+   r,   r/   r4   r:   r<   r   r	   rC   rO   rS   rV   r[   r`   re   r   r   r   r   r    G   s   

	

r    c               @   s`   e Zd Zdd Zeje ddd Zeje ddd Zdd	 Z	d
d Z
dd Zdd ZdS )TestSensorsBatteryc             C   s.   t j d r| jtj  n| jtj  d S )NZSystemBatteriesPresent)r1   ZGetPwrCapabilitiesZassertIsNotNoner   sensors_batteryassertIsNone)r   r   r   r   test_has_battery   s    z#TestSensorsBattery.test_has_batteryz
no batteryc             C   s6   t j }|jdd }tj }| j|j|jdd d S )Nzselect * from Win32_Batteryr   rQ   )rP   )r5   r6   queryr   rk   rR   ZpercentZEstimatedChargeRemaining)r   r8   battery_wmibattery_psutilr   r   r   test_percent   s    zTestSensorsBattery.test_percentc             C   s6   t j }|jdd }tj }| j|j|jdk d S )Nzselect * from Win32_Batteryr   r   )r5   r6   rn   r   rk   r-   Zpower_pluggedZBatteryStatus)r   r8   ro   rp   r   r   r   test_power_plugged   s
    z%TestSensorsBattery.test_power_pluggedc          	   C   s6   t jddd}| jtj  |js(tW d Q R X d S )Nz&psutil._pswindows.cext.sensors_batteryr      )ra   )r   rs   r   r   )r   rd   rl   r   rk   calledAssertionError)r   mr   r   r   test_emulate_no_battery   s    
z*TestSensorsBattery.test_emulate_no_batteryc          
   C   s<   t jddd$}| jtj jtj |js.tW d Q R X d S )Nz&psutil._pswindows.cext.sensors_batteryrQ   r   )ra   )rQ   r   r   r   )	r   rd   r-   r   rk   secsleftPOWER_TIME_UNLIMITEDrt   ru   )r   rv   r   r   r   test_emulate_power_connected   s
    
z/TestSensorsBattery.test_emulate_power_connectedc          
   C   s<   t jddd$}| jtj jtj |js.tW d Q R X d S )Nz&psutil._pswindows.cext.sensors_batteryr      )ra   )r   r{   r   r   )	r   rd   r-   r   rk   rx   ry   rt   ru   )r   rv   r   r   r   test_emulate_power_charging   s
    
z.TestSensorsBattery.test_emulate_power_chargingc          
   C   s<   t jddd$}| jtj jtj |js.tW d Q R X d S )Nz&psutil._pswindows.cext.sensors_batteryr   rQ   )ra   )r   r   r   r}   )	r   rd   r-   r   rk   rx   ZPOWER_TIME_UNKNOWNrt   ru   )r   rv   r   r   r   test_emulate_secs_left_unknown  s
    
z1TestSensorsBattery.test_emulate_secs_left_unknownN)rf   rg   rh   rm   r   ri   r   rq   rr   rw   rz   r|   r~   r   r   r   r   rj      s   	
rj   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ejejd+k d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   s   t  j| _d S )N)r   pid)clsr   r   r   
setUpClass  s    zTestProcess.setUpClassc             C   s
   t   d S )N)r   )r   r   r   r   tearDownClass  s    zTestProcess.tearDownClassc             C   s   t jd}| jt j|j d S )Nr   )r   ProcessassertRaisesr   kill)r   pr   r   r   test_issue_24  s    
zTestProcess.test_issue_24c             C   s   t jd}| j|j d t| |j  | j|j dk y|j d d \}}W n( t j	k
r|   t
j d d
krx Y nX | j|d	k d S )Nrb   ZSystemg        r   rQ   vistawin-7win7r   )r   r   r   )r   r   r-   namestrusernamerZ   create_timememory_infor   platformuname)r   r   rssvmsr   r   r   test_special_pid   s    
zTestProcess.test_special_pidc             C   s"   t j| j}| jt|jtj d S )N)r   r   r   r   
ValueErrorsend_signalsignalSIGINT)r   r   r   r   r   test_send_signal1  s    zTestProcess.test_send_signalc             C   sN   xHt j D ]<}y | jtjj|j |j  W q
 t jk
rD   Y q
X q
W d S )N)	r   process_iterr-   r+   pathbasenameexer   Error)r   r   r   r   r   test_exe5  s
     zTestProcess.test_exec             C   sb   t jtj }|j }tjtjtj	tj }|j }| j
||d  tj| | j
|j | d S )NrQ   )r   r   r+   getpidnum_handlesr1   OpenProcesswin32conPROCESS_QUERY_INFORMATIONFALSEr-   CloseHandle)r   r   Zbeforehandleafterr   r   r   test_num_handles_increment<  s    
z&TestProcess.test_num_handles_incrementc                s    fdd}t j| j}g }xtt jD ]  jds( dkrBq(q(y(||  |j }||  |j }W n t jt jfk
r   Y q(X ||kr(d ||f }|j| q(W |r| j	ddj
|  d S )Nc                s,   t |  d }|d k	r$t|r$|  n| d S )N)getattrr   )r   attr)r   r   r   callJ  s    z+TestProcess.test_handles_leak.<locals>.call_	terminater   suspendresumenicer   waitchildrenas_dictmemory_info_exz@failure while processing Process.%s method (before=%s, after=%s)
)
r   r   r   r   r   r   r   r   r   r   )r   r   r   dirrT   r   r   r   appendr&   join)r   r   r   ZfailuresZnum1Znum2r&   r   )r   r   test_handles_leakF  s,    
 

zTestProcess.test_handles_leakc             C   s:   x4t j D ](}y|j  W q
 t jk
r0   Y q
X q
W d S )N)r   r   r   r   )r   r   r   r   r   test_name_always_availablej  s
    z&TestProcess.test_name_always_availabler      zCTRL_* signals not supportedc             C   sb   t jt j}|jtj |jtj |j  |j	  | j
t j|jtj | j
t j|jtj d S )N)r   r   r   r   r   r   ZCTRL_C_EVENTZCTRL_BREAK_EVENTr   r   r   r   )r   r   r   r   r   test_ctrl_signalss  s    zTestProcess.test_ctrl_signalsc             C   s\   xVt j D ]J}ytjj|j }|j }W n t jt jfk
rF   Y q
X | j	|| q
W d S )N)
r   r   r+   r   r   r   r   r   r   r-   )r   r   abr   r   r   test_compare_name_exe  s    z!TestProcess.test_compare_name_exec             C   s    | j tj j tjtj d S )N)r-   r   r   r   r1   ZGetUserNameExr   ZNameSamCompatible)r   r   r   r   test_username  s    zTestProcess.test_usernamec             C   s8   t jddtj j }djtj j }| j	|| d S )Nz +r!   )
resubr1   ZGetCommandLinestripr   r   r   cmdliner-   )r   r2   r3   r   r   r   test_cmdline  s    zTestProcess.test_cmdlinec             C   sJ   t jtjtjtj }| jt j| t	j
|}tj j }| j|| d S )N)r1   r   r   r   r   r+   r   
addCleanupr   win32processZGetPriorityClassr   r   r   r-   )r   r   r2   r3   r   r   r   	test_nice  s    
zTestProcess.test_nicec             C   s   t jtjtj| j}| jt j| tj	|}t
j| jj }| 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|j d S )	NZPeakWorkingSetSizeWorkingSetSizeZQuotaPeakPagedPoolUsageZQuotaPagedPoolUsageZQuotaPeakNonPagedPoolUsageZQuotaNonPagedPoolUsageZPagefileUsageZPeakPagefileUsage)r1   r   r   r   r   r   r   r   r   ZGetProcessMemoryInfor   r   r   r-   Z	peak_wsetZwsetZpeak_paged_poolZ
paged_poolZpeak_nonpaged_poolZnonpaged_poolZpagefileZpeak_pagefiler   r   )r   r   r2   r3   r   r   r   test_memory_info  s0    
zTestProcess.test_memory_infoc             C   sX   t jtjtj| j}| jt j| tj	| j}|j
  |j }tj|}| j|| d S )N)r1   r   r   r   r   r   r   r   r   r   r   r   r   ZGetExitCodeProcessr-   )r   r   r   r3   r2   r   r   r   	test_wait  s    
zTestProcess.test_waitc             C   s\   dd }t jtjtj| j}| jt j| |tj	|d }t
j| jj }| j|| d S )Nc                s    fddt dD S )Nc                s   g | ]}d |>  @ r|qS )rQ   r   )r>   i)r?   r   r   r@     s    zGTestProcess.test_cpu_affinity.<locals>.from_bitmask.<locals>.<listcomp>@   )range)r?   r   )r?   r   from_bitmask  s    z3TestProcess.test_cpu_affinity.<locals>.from_bitmaskr   )r1   r   r   r   r   r   r   r   r   ZGetProcessAffinityMaskr   r   Zcpu_affinityr-   )r   r   r   r2   r3   r   r   r   test_cpu_affinity  s    zTestProcess.test_cpu_affinityc             C   s   t jtjtjtj }| jt j| t	j
|}tj j }| j|j|d  | j|j|d  | j|j|d  | j|j|d  | j|j|d  | j|j|d  d S )NZReadOperationCountZWriteOperationCountZReadTransferCountZWriteTransferCountZOtherOperationCountZOtherTransferCount)r1   r   r   r   r   r+   r   r   r   r   ZGetProcessIoCountersr   r   io_countersr-   Z
read_countZwrite_countZ
read_bytesZwrite_bytesother_countZother_bytes)r   r   r2   r3   r   r   r   test_io_counters  s"    
zTestProcess.test_io_countersc             C   s   dd l }dd l}d}|jjj|dtj }| j|jjj| |j	j
 }|jjj||j| |j}tj j }|jjj| | j||d  d S )Nr   i   rQ   )ctypesZctypes.wintypesZwindllZkernel32r   r+   r   r   r   ZwintypesZDWORDZGetProcessHandleCountZbyrefvaluer   r   r   r-   )r   r   r   r   Zhndcntr2   r3   r   r   r   test_num_handles  s    
zTestProcess.test_num_handlesN)r   r   )rf   rg   rh   classmethodr   r   r   r   r   r   r   r   r   r   ri   sysversion_infor   r   r   r   r   r   r   r   r   r   r   r   r   r   r     s(   
$	

r   c               @   s`   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d ZdS )TestProcessWMIz%Compare Process API results with WMI.c             C   s   t  j| _d S )N)r   r   )r   r   r   r   r     s    zTestProcessWMI.setUpClassc             C   s
   t   d S )N)r   )r   r   r   r   r     s    zTestProcessWMI.tearDownClassc             C   s8   t j j| jdd }tj| j}| j|j |j d S )N)r=   r   )	r5   r6   rA   r   r   r   r-   r   ZCaption)r   r8   r   r   r   r   	test_name
  s    zTestProcessWMI.test_namec             C   s@   t j j| jdd }tj| j}| j|j j |j	j  d S )N)r=   r   )
r5   r6   rA   r   r   r   r-   r   r%   ZExecutablePath)r   r8   r   r   r   r   r     s    zTestProcessWMI.test_exec             C   sF   t j j| jdd }tj| j}| jdj|j |j	j
dd d S )N)r=   r   r!   "rF   )r5   r6   rA   r   r   r   r-   r   r   ZCommandLiner$   )r   r8   r   r   r   r   r     s    zTestProcessWMI.test_cmdlinec             C   sP   t j j| jdd }tj| j}|j \}}}d||f }| j|j | d S )N)r=   r   z%s\%s)	r5   r6   rA   r   r   r   ZGetOwnerr-   r   )r   r8   r   Zdomainr   r   r   r   r   r     s
    zTestProcessWMI.test_usernamec             C   sL   t jd tj j| jdd }tj| j}|j j	}| j
|t|j d S )Ng?)r=   r   )timesleepr5   r6   rA   r   r   r   r   r   r-   r*   r   )r   r8   r   r   r   r   r   test_memory_rss#  s
    

zTestProcessWMI.test_memory_rssc             C   sj   t jd tj j| jdd }tj| j}|j j	}t
|j}||krf||d krf| jd||f  d S )Ng?)r=   r   i   zwmi=%s, psutil=%s)r   r   r5   r6   rA   r   r   r   r   r   r*   ZPageFileUsager&   )r   r8   r   r   Z	wmi_usager   r   r   test_memory_vms*  s    


zTestProcessWMI.test_memory_vmsc             C   s\   t j j| jdd }tj| j}t|jjdd }t	j
dt	j|j }| j|| d S )N)r=   r   r\   z%Y%m%d%H%M%S)r5   r6   rA   r   r   r   r   ZCreationDaterU   r   ZstrftimeZ	localtimer   r-   )r   r8   r   Zwmic_createZpsutil_creater   r   r   test_create_time7  s    zTestProcessWMI.test_create_timeN)rf   rg   rh   __doc__r   r   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   s   t  j| _d S )N)r   r   )r   r   r   r   r   L  s    z(TestDualProcessImplementation.setUpClassc             C   s
   t   d S )N)r   )r   r   r   r   r   P  s    z+TestDualProcessImplementation.tearDownClassc          
   C   sZ   t j| jj }tjdt jtj d(}| j	t j| jj | |j
sLtW d Q R X d S )Nz psutil._psplatform.cext.proc_exe)side_effect)r   r   r   r   r   rd   r   r+   r   r-   rt   ru   )r   r   r   r   r   r   r   W  s
    z'TestDualProcessImplementation.test_namec             C   s   t j| jj }tjdttjdd}t j| jj }| j	t
|t
| xLtt
|D ]<}| j|| d | j|| d | j|| || dd qZW |jstW d Q R X d S )Nz(psutil._psplatform.cext.proc_memory_infomsg)r   r   i   )rP   )r   r   r   r   r   rd   r   r   EPERMr-   lenr   ZassertGreaterEqualrR   rt   ru   )r   Zmem_1r   Zmem_2r   r   r   r   r   ^  s    z.TestDualProcessImplementation.test_memory_infoc          
   C   sX   t j| jj }tjdttjdd(}| j	t j| jj | |j
sJtW d Q R X d S )Nz(psutil._psplatform.cext.proc_create_timer   )r   )r   r   r   r   r   rd   r   r   r   r-   rt   ru   )r   Zctimer   r   r   r   r   j  s
    z.TestDualProcessImplementation.test_create_timec             C   sx   t j| jj }tjdttjddH}t j| jj }|j	sBt
| j|j|jdd | j|j|jdd W d Q R X d S )Nz&psutil._psplatform.cext.proc_cpu_timesr   )r   g{Gz?)rP   )r   r   r   Z	cpu_timesr   rd   r   r   r   rt   ru   rR   usersystem)r   Zcpu_times_1r   Zcpu_times_2r   r   r   test_cpu_timesq  s    
z,TestDualProcessImplementation.test_cpu_timesc             C   s~   t j| jj }tjdttjddN}t j| jj }x,t	t
|D ]}| j|| || dd qFW |jsptW d Q R X d S )Nz(psutil._psplatform.cext.proc_io_countersr   )r   r0   )rP   )r   r   r   r   r   rd   r   r   r   r   r   rR   rt   ru   )r   Zio_counters_1r   Zio_counters_2r   r   r   r   r   |  s    z.TestDualProcessImplementation.test_io_countersc          
   C   sX   t j| jj }tjdttjdd(}| j	t j| jj | |j
sJtW d Q R X d S )Nz(psutil._psplatform.cext.proc_num_handlesr   )r   )r   r   r   r   r   rd   r   r   r   r-   rt   ru   )r   r   r   r   r   r   r     s    z.TestDualProcessImplementation.test_num_handlesN)rf   rg   rh   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   @  s   

r   c               @   s|   e Zd ZdZedd Zedd ZddgZdd	 Z	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 )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   sT   d} xJt j dD ]<}tj|d| gtjtjd}|j \}}|tt kr|S qW d S )Nz6import sys; sys.stdout.write(str(sys.maxsize > 2**32))zC:\Python*\python.exez-c)r   stdoutstderr)glob
subprocessPopenPIPEZSTDOUTcommunicater   	IS_64_BIT)codefilenamer9   outputr   r   r   r   find_other_interpreter  s    
z,RemoteProcessTestCase.find_other_interpreterc             C   s@   | j  }|d krtjdtr.tj| _|| _n|| _tj| _d S )Nz0could not find interpreter with opposite bitness)r   r   ZSkipTestr   r   
executablepython64python32)r   Zother_pythonr   r   r   r     s    z RemoteProcessTestCase.setUpClassz-czimport sys; sys.stdin.read()c             C   sV   t jj }tt j |d< t| jg| j |tj	d| _
t| jg| j |tj	d| _d S )NTHINK_OF_A_NUMBER)envstdin)r+   r,   copyr   r   r   r   	test_argsr   r   proc32r   proc64)r   r   r   r   r   setUp  s    
zRemoteProcessTestCase.setUpc             C   s   | j j  | jj  t  d S )N)r  r   r  r   )r   r   r   r   tearDown  s    

zRemoteProcessTestCase.tearDownc             C   s
   t   d S )N)r   )r   r   r   r   r     s    z#RemoteProcessTestCase.tearDownClassc             C   s@   t j| jj}| jt|j d | j|j dd  | j d S )Nr]   rQ   )r   r   r  r   r-   r   r   r  )r   r   r   r   r   test_cmdline_32  s    z%RemoteProcessTestCase.test_cmdline_32c             C   s@   t j| jj}| jt|j d | j|j dd  | j d S )Nr]   rQ   )r   r   r  r   r-   r   r   r  )r   r   r   r   r   test_cmdline_64  s    z%RemoteProcessTestCase.test_cmdline_64c             C   s&   t j| jj}| j|j tj  d S )N)r   r   r  r   r-   cwdr+   getcwd)r   r   r   r   r   test_cwd_32  s    z!RemoteProcessTestCase.test_cwd_32c             C   s&   t j| jj}| j|j tj  d S )N)r   r   r  r   r-   r  r+   r	  )r   r   r   r   r   test_cwd_64  s    z!RemoteProcessTestCase.test_cwd_64c             C   s>   t j| jj}|j }| jd| | j|d ttj	  d S )Nr   )
r   r   r  r   r,   assertInassertEqualsr   r+   r   )r   r   er   r   r   test_environ_32  s    z%RemoteProcessTestCase.test_environ_32c             C   s>   t j| jj}|j }| jd| | j|d ttj	  d S )Nr   )
r   r   r  r   r,   r  r  r   r+   r   )r   r   r  r   r   r   test_environ_64  s    z%RemoteProcessTestCase.test_environ_64N)rf   rg   rh   r   staticmethodr   r   r   r  r  r  r   r  r  r
  r  r  r  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 dddddddg}t dd	d
g}t dddddddg}x,tj D ]}|j }| j|d t | j|d j d | j|d t | j|d t | j|d | |d d k	rtj	|d  | j|d t | j|d t | j|d t | j|d | | j|d | | j|d t |j
 }|d k	rLtj	|}| j|j  tj|j }| j|| qFW d S )NZrunningZpausedstartpausecontinuestopZstoppedZ	automaticZmanualZdisabledZstart_pendingZpause_pendingZcontinue_pendingZstop_pendingr   rF   display_namer   statusr   ZbinpathZ
start_typedescription)rB   r   win_service_iterr   ZassertIsInstancer   ZassertNotEqualr   r  r   r   rZ   Z
is_runningwin_service_getr   r-   )r   Zvalid_statusesZvalid_start_typesZservdatar   r   sr   r   r   test_win_service_iter  sR    

z"TestServices.test_win_service_iterc          '   C   sp  t tj j }| jtj}tj|d  W d Q R X | j|jj|d  tj|}t	tj
jjd}tjd|d | jtj|j W d Q R X tjd|d | jtj|j W d Q R X t	tj
jjd}tjd|d | jtj|j W d Q R X tjd|d | jtj|j W d Q R X | j|j t| | j|j t| | j|j t| | j|j t| d S )Nz???rF   z/psutil._psplatform.cext.winservice_query_status)r   z/psutil._psplatform.cext.winservice_query_config)nextr   r  r   r   r   r  r-   Z	exceptionZWindowsError_psplatformrW   ZERROR_SERVICE_DOES_NOT_EXISTr   rd   r  r   ZERROR_ACCESS_DENIEDr   r  r   r  rN   )r   r   cmZserviceexcr   r   r   test_win_service_get!  s2    




z!TestServices.test_win_service_getN)rf   rg   rh   r  r#  r   r   r   r   r    s   0r  __main__l        )2r   r^   r   r   r+   r   r   r   r   r   r   warningsr   r   Zpsutil._compatr   Zpsutil.testsr   r   r   r   r   r	   r
   r   r   catch_warningssimplefilterr1   r   r   r5   ImportErrorr   r   rW   maxsizer   r   ri   ZTestCaser    rj   r   r   r   r   r  rf   __file__r   r   r   r   <module>   sl   



 
<
 m
A
N
^
U
