
    /h#C                        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m	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 erddlZddlZddlm Z  d$dZ!d Z"d Z#d Z$d Z%d Z&ej'        (                    e d           G d de                      Z)ej'        (                    e d           G d  d!e                      Z*ej'        (                    e d           G d" d#e                      Z+dS )%zPOSIX specific tests.    N)mock)AIX)BSD)LINUX)MACOS)OPENBSD)POSIX)SUNOS)AARCH64)HAS_NET_IO_COUNTERS)
PYTHON_EXE)PsutilTestCase)pytest)retry_on_failure)sh)skip_on_access_denied)spawn_testproc)	terminate)getpagesizec                    dg}t           r|                    d           |%|                    dt          |          g           n9t          st
          r|                    d           n|                    d           t          r|                     dd          } |                    d	| g           t          |          }t           r|                                n|                                d
d         }g }|D ]L}|	                                }	 t          |          }n# t          $ r Y nw xY w|                    |           M||S |d         S )zwWrapper for calling the ps command with a little bit of cross-platform
    support for a narrow range of features.
    psz--no-headersNz-pz-Aaxstartstimez-o   r   )r   appendextendstrr
   r   replacer   
splitlinesstripint
ValueError)fmtpidcmdoutput
all_outputlines         J/var/www/html/venv/lib/python3.11/site-packages/psutil/tests/test_posix.pyr   r   -   sm   
 &C #

>"""


D#c((#$$$$	 # 

4

4 ,kk'7++JJc{WWF$)FV   v/@/@/B/B122/FFJ    zz||	t99DD 	 	 	D	 	$
{!}s   D%%
D21D2c                 l    d}t           rd}t          ||                                           }|d         S )Ncommandcommr   )r
   r   split)r%   fieldr,   s      r*   ps_namer0   d   s7    E nn""$$G1:    c                     d}t           st          rd}t          ||           }t          j        dd|          }|                                S )Nr,   argsz\(python.*?\)$ )r   r
   r   resubr!   )r%   r/   outs      r*   ps_argsr8   l   sJ    E
 e 
UC..C
&"B
,
,C99;;r1   c                 8    d}t           rd}t          ||           S )Nrssrssizer   r   r%   r/   s     r*   ps_rssr>   v   s"    E
 eS>>r1   c                 8    d}t           rd}t          ||           S )Nvszvsizer<   r=   s     r*   ps_vszrB   }   s"    E
 eS>>r1   c                 .   	 t          d|                                            }nJ# t          $ r=}dt          |                                          v rt          j        d           d }~ww xY w|                    d          d         }|                                }t          |d                   dz  }t          |d                   dz  }t          |d                   dz  }t          |d	         
                    d
d                    }||||fS )Nzdf -k zdevice busyzdf returned EBUSY
r               %r4   )r   r!   RuntimeErrorr   lowerr   skipr.   r"   floatr   )	devicer7   errr)   fields	sys_totalsys_usedsys_freesys_percents	            r*   dfrU      s   "&""##))++   CHHNN,,,,+1222 99T??1DZZ\\FF1I%I6!9~~$H6!9~~$Hq	))#r2233Kx;77s   $' 
A.8A))A.z
POSIX onlyreasonc                      e Zd ZdZed             Zed             Zd Zd Zd Z	d Z
d Z e             e            d	                         Z e             e            d
                         Zd Zd Zd Zd Zej                            eped          d             Zd Z e            d             Zej                            ed          ej                            ed          d                         ZdS )TestProcesszBCompare psutil results against 'ps' command line utility (mainly).c                 `    t          t          ddgt          j                  j        | _        d S )Nz-Ez-O)stdin)r   r   
subprocessPIPEr%   clss    r*   
setUpClasszTestProcess.setUpClass   s0     t$JO
 
 

 	r1   c                 .    t          | j                   d S N)r   r%   r^   s    r*   tearDownClasszTestProcess.tearDownClass   s    #'r1   c                     t          d| j                  }t          j        | j                                                  }||k    sJ d S )Nppid)r   r%   psutilProcessre   )selfppid_psppid_psutils      r*   	test_ppidzTestProcess.test_ppid   sG    VTX&&nTX..3355+%%%%%%r1   c                     t          d| j                  }t          j        | j                                                  j        }||k    sJ d S )Nuid)r   r%   rf   rg   uidsreal)rh   uid_ps
uid_psutils      r*   test_uidzTestProcess.test_uid   sJ    E48$$^DH--22449
######r1   c                     t          d| j                  }t          j        | j                                                  j        }||k    sJ d S )Nrgid)r   r%   rf   rg   gidsro   )rh   gid_ps
gid_psutils      r*   test_gidzTestProcess.test_gid   sJ    FDH%%^DH--22449
######r1   c                     t          d| j                  }t          j        | j                                                  }||k    sJ d S )Nuser)r   r%   rf   rg   username)rh   username_psusername_psutils      r*   test_usernamezTestProcess.test_username   sG    ** .22;;==o------r1   c                 $   t          j                    }t          j        dt                    5 }|                                t          |                                j                  k    sJ |j	        sJ 	 d d d            d S # 1 swxY w Y   d S )Nzpsutil.pwd.getpwuidside_effect)
rf   rg   r   patchKeyErrorr{   r   rn   ro   called)rh   pfuns      r*   test_username_no_resolutionz'TestProcess.test_username_no_resolution   s     NZ-8DDD 	::<<3qvvxx}#5#55555:	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   ABB	B	c                     t          j        d           t          | j                  }t	          j        | j                                                  d         dz  }||k    sJ d S )N皙?r   rE   )timesleepr>   r%   rf   rg   memory_info)rh   rss_ps
rss_psutils      r*   test_rss_memoryzTestProcess.test_rss_memory   ^    
 	
3!!^DH--99;;A>E
######r1   c                     t          j        d           t          | j                  }t	          j        | j                                                  d         dz  }||k    sJ d S )Nr   r   rE   )r   r   rB   r%   rf   rg   r   )rh   vsz_ps
vsz_psutils      r*   test_vsz_memoryzTestProcess.test_vsz_memory   r   r1   c                    t          | j                  }t          j                            |                                          }t          j        | j                                                                                  }t          j
        dd|          }t          j
        dd|          }t          j
        dd|          }t          j
        dd|          }||k    sJ d S )Nz\d.\dr4   z\d)r0   r%   ospathbasenamerK   rf   rg   namer5   r6   )rh   name_psname_psutils      r*   	test_namezTestProcess.test_name   s    $(##'""7++1133nTX..3355;;== &2w//fXr;77&G,,fUB44+%%%%%%r1   c                 (   d}g d}t          j        d|          5  t          j        d|          5  t          j                    }|                                dk    sJ 	 d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S )Nlong-program-name)long-program-name-extendedfoobarpsutil._psplatform.Process.namereturn_value"psutil._psplatform.Process.cmdliner   )r   r   rf   rg   r   )rh   r   cmdliner   s       r*   test_name_longzTestProcess.test_name_long   sP    #>>>Z9MMM 	@ 	@47   @ @ N$$vvxx#??????	@ @ @ @ @ @ @ @ @ @ @ @ @ @ @	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@s4   B.A/#B/A3	3B6A3	7BBBc                 F   d}t          j        d|          5  t          j        dt          j        dd                    5  t          j                    }|                                dk    sJ 	 d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S Nr   r   r   r   r   r4   r   )r   r   rf   AccessDeniedrg   r   rh   r   r   s      r*   test_name_long_cmdline_ad_excz)TestProcess.test_name_long_cmdline_ad_exc   s.    #Z9MMM 	7 	74"/266   7 7 N$$vvxx#6666667 7 7 7 7 7 7 7 7 7 7 7 7 7 7	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7s5   *B.A>2B>B	BB	BBBc                    d}t          j        d|          5  t          j        dt          j        dd                    5  t          j                    }t          j        t          j                  5  |                                 d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S r   )r   r   rf   NoSuchProcessrg   r   raisesr   r   s      r*   test_name_long_cmdline_nsp_excz*TestProcess.test_name_long_cmdline_nsp_exc   s    #Z9MMM 	 	4"0B77     N$$]6#788  FFHHH                            	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	sY   *C2B-5B
B-BB-BB-!C-B1	1C4B1	5CC	C	zps -o start not availablerV   c                 |   t          d| j                  }t          j        | j                                                  }t
          j                            |                              d          }t          |          }t
          j                            |                              d          }|||hv sJ d S )Nr   z%H:%M:%S)	r   r%   rf   rg   create_timedatetimefromtimestampstrftimeround)rh   time_pstime_psutiltime_psutil_tstampround_time_psutilround_time_psutil_tstamps         r*   test_create_timezTestProcess.test_create_time  s    Wdh''nTX..::<<%.<<
 

(:

 	
 "+..#+#4#B#B$
 $

(:

 	! -/GHHHHHHHr1   c                     t          | j                  }t          j        | j                                                  }	 ||k    sJ d S # t
          $ r# |d t          |                   }||k    sJ Y d S w xY wrb   )r0   r%   rf   rg   exeAssertionErrorlen)rh   ps_pathnamepsutil_pathnameadjusted_ps_pathnames       r*   test_exezTestProcess.test_exe  s    dh'' .226688
	7/111111 	7 	7 	7 $//A[1A1A/A#B "66666666	7s   A )A87A8c                 :   t          | j                  }d                    t          j        | j                                                            }t          r9t          |          t          |          k     r|                    |          sJ d S ||k    sJ d S )N )	r8   r%   joinrf   rg   r   r   r   
startswith)rh   
ps_cmdlinepsutil_cmdlines      r*   test_cmdlinezTestProcess.test_cmdline+  s    TX&&
&.":":"B"B"D"DEE 	0s:^)<)<<<!,,Z8888888//////r1   znot reliable on SUNOSznot reliable on AIXc                     t          d| j                  }t          j                                                    }||k    sJ d S )Nnice)r   r%   rf   rg   r   )rh   ps_nicepsutil_nices      r*   	test_nicezTestProcess.test_nice9  sE     VTX&&n&&++--+%%%%%%r1   N)__name__
__module____qualname____doc__classmethodr`   rc   rk   rr   rx   r~   r   r   r   r   r   r   r   r   r   r   markskipifr   r   r   r   r   r
   r   r    r1   r*   rY   rY      s       LL  [
   [& & &
$ $ $
$ $ $
. . .
   $ $  $ $ $  $& & &@ @ @7 7 7   [-HIII I JII7 7 7( 0 0 0 [&=>>[$9::& & ;: ?>& & &r1   rY   c                      e Zd ZdZ e            d             Zej                            e	d          ej                             e
j        d           d          ej                            e d          d                                     Z e            d	             Z e            d
             Zd Zd Zd Zd Zej                            ed           e            d                         ZdS )TestSystemAPIszTest some system APIs.c                    t          t          d                    t          j                    t          st
          rdvr                    dd           t                    t                    z
  dk    rBfdD             fdD             z   }|                     dt          |          z             d S )Nr%   r   r   c                     g | ]}|v|	S r   r   ).0xpids_pss     r*   
<listcomp>z,TestSystemAPIs.test_pids.<locals>.<listcomp>R  s#    EEEAW4D4D!4D4D4Dr1   c                     g | ]}|v|	S r   r   )r   r   pids_psutils     r*   r   z,TestSystemAPIs.test_pids.<locals>.<listcomp>R  s-     I I Ia{&:&:&:&:&:r1   zdifference: )
sortedr   rf   pidsr   r   insertr   failr   )rh   
differencer   r   s     @@r*   	test_pidszTestSystemAPIs.test_pidsE  s     E##kmm  	!W 	!'!1!1NN1a    w<<#k***Q..EEEE[EEE I I I I"I I I J ))NS__<===	 /.r1   zunreliable on SUNOSrV   ifconfigzno ifconfig cmdznot supportedc                     t          d          }t          j        d          D ]K}|                                D ]}|                    |          r n|                     d| d|           Ld S )Nzifconfig -aT)perniczcouldn't find z nic in 'ifconfig -a' output
)r   rf   net_io_countersr.   r   r   )rh   r'   nicr)   s       r*   test_nic_nameszTestSystemAPIs.test_nic_namesY  s     M"")666 	 	C  ??3'' E ii)S ) ) &) )   	 	r1   c                    t          d          }|                                st          j        d          |                    d          }d |D             }d |D             }t          |          t          t          j                              k    sJ |                     t          j                    |          5  t          t          j                              D ]K\  }}|j
        ||         k    sJ |j        ||         k    sJ |j        t          j        |j                   L	 d d d            d S # 1 swxY w Y   d S )Nwho -uno users on this systemrD   c                 B    g | ]}|                                 d          S )r   r.   r   r   s     r*   r   z-TestSystemAPIs.test_users.<locals>.<listcomp>p  s$    ---!1---r1   c                 B    g | ]}|                                 d          S )r   r   r   s     r*   r   z-TestSystemAPIs.test_users.<locals>.<listcomp>q  s$    111aQWWYYq\111r1   rf   who)r   r!   r   rL   r.   r   rf   userssubTest	enumerater   terminalr%   rg   )rh   r7   linesr   	terminalsidxus          r*   
test_userszTestSystemAPIs.test_usersj  sw   llyy{{ 	9+7888		$--u---115111	5zzS000000\\S\99 	* 	*#FLNN33 * *Qvs++++zYs^33335$N15)))	*	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	*s    A-D;;D?D?c                    t          d          }|                                st          j        d          d }t	          j        d|          }|rd}nYt	          j        d|          }|rd}n?t	          j        d|          }|rd}n%t	          j        d	|          }|rd}d
 |D             }|st          j        d|           |                     t          j                    |          5  t          t          j                              D ]J\  }}t          j
                            |j                                      |          }|||         k    sJ K	 d d d            d S # 1 swxY w Y   d S )Nr   r   z\d\d\d\d-\d\d-\d\d \d\d:\d\dz%Y-%m-%d %H:%Mz[A-Z][a-z][a-z] \d\d \d\d:\d\dz%b %d %H:%Mz[A-Z][a-z][a-z] \d\dz%b %dz[a-z][a-z][a-z] \d\dc                 6    g | ]}|                                 S r   )
capitalizer   s     r*   r   z5TestSystemAPIs.test_users_started.<locals>.<listcomp>  s     "C"C"Ca1<<>>"C"C"Cr1   z&cannot interpret tstamp in who output
r   )r   r!   r   rL   r5   findallr   rf   r   r   r   r   startedr   )rh   r7   tstampr  r   r   psutil_values          r*   test_users_startedz!TestSystemAPIs.test_users_startedz  s   llyy{{ 	9+7888*<cBB 	D%FF j!BCHHG D& *%<cBB D$FF !j)@#FFG D!("C"C7"C"C"C 	O+MMMNNN\\S\99 	4 	4#FLNN33 4 4Q'0>>I   (6""  $ws|33333	4	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4s   .A,E((E,/E,c                 j   t          j        dt          t          j        d                    5 }t          j        t                    5  t          j        	                    t          j                               d d d            n# 1 swxY w Y   |j        sJ 	 d d d            d S # 1 swxY w Y   d S )Nzpsutil._psposix.os.killr4   r   )r   r   OSErrorerrnoEBADFr   r   rf   _psposix
pid_existsr   getpidr   rh   ms     r*   test_pid_exists_let_raisez(TestSystemAPIs.test_pid_exists_let_raise  s    Z%75;3K3K
 
 
 	w'' 8 8**29;;7778 8 8 8 8 8 8 8 8 8 8 8 8 8 88OOOO	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	5   B(	1B:B(B
	
B(B
	B((B,/B,c                 j   t          j        dt          t          j        d                    5 }t          j        t                    5  t          j        	                    t          j                               d d d            n# 1 swxY w Y   |j        sJ 	 d d d            d S # 1 swxY w Y   d S )Npsutil._psposix.os.waitpidr4   r   )r   r   r
  r  r  r   r   rf   r  wait_pidr   r  r   r  s     r*   test_os_waitpid_let_raisez(TestSystemAPIs.test_os_waitpid_let_raise  s    Z(gek26N6N
 
 
 	w'' 6 6((5556 6 6 6 6 6 6 6 6 6 6 6 6 6 68OOOO	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	r  c                    t          j        dt          t          j        d                    5 }t          j        t          j        j	                  5  t          j        
                    t          j                    d           d d d            n# 1 swxY w Y   |j        sJ 	 d d d            d S # 1 swxY w Y   d S )Nr  r4   r   g{Gz?)timeout)r   r   r
  r  EINTRr   r   rf   r  TimeoutExpiredr  r   r  r   r  s     r*   test_os_waitpid_eintrz$TestSystemAPIs.test_os_waitpid_eintr  s5   Z(gek26N6N
 
 
 	v=>> D D((d(CCCD D D D D D D D D D D D D D D8OOOO	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s5   $B43BB4B	B4B	B44B8;B8c                 :   t          j        dd          5 }t          j        t                    5  t
          j                            t          j	                               d d d            n# 1 swxY w Y   |j
        sJ 	 d d d            d S # 1 swxY w Y   d S )Nr  )r   r   )r   r   r   r   r#   rf   r  r  r   r  r   r  s     r*   test_os_waitpid_bad_ret_statusz-TestSystemAPIs.test_os_waitpid_bad_ret_status  s   Z(w
 
 
 	z** 6 6((5556 6 6 6 6 6 6 6 6 6 6 6 6 6 68OOOO	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s4   B1A."B.A2	2B5A2	6BBBzunreliable on AIXc                    d}t          j        d          D ]}t          j        |j                  }	 t	          |j                  \  }}}}t          |j        |z
            |k     sJ t          |j        |z
            |k     sJ t          |j	        |z
            |k     sJ t          |j
        |z
            dk    sJ # t          $ r8}t          |                                          }d|v sd|v sd|v rY d }~ d }~ww xY wd S )Ni  @ F)allr   zno such file or directoryzraw devices not supportedzpermission denied)rf   disk_partitions
disk_usage
mountpointrU   rN   abstotalusedfreepercentrJ   r   rK   )	rh   	tolerancepartusagerQ   rR   rS   rT   rO   s	            r*   test_disk_usagezTestSystemAPIs.test_disk_usage  sI    $	*u555 	= 	=D%do66E==?__:	8X{ 5;233i????5:011I====5:011I====5=;6771<<<<<!      #hhnn&&/3662c99*c11HHHH		= 	=s   C
D-C?>C??DN)r   r   r   r   r   r   r   r   r   r
   shutilwhichr   r   r   r  r  r  r  r  r   r-  r   r1   r*   r   r   A  s         > > >& [&;<<[LFL444=NOO[//HH
 
 IH PO =<
 * * * "4 "4 "4H	 	 	       [$788= =  98= = =r1   r   c                       e Zd Zd ZdS )TestMiscc                     t                      }|dk    sJ |t          j                     k    sJ |t          j        k    sJ d S )Nr   )r   resourcemmapPAGESIZE)rh   pagesizes     r*   test_getpagesizezTestMisc.test_getpagesize  sK    ==!||||8/1111114=((((((r1   N)r   r   r   r7  r   r1   r*   r1  r1    s#        ) ) ) ) )r1   r1  rb   ),r   r   r  r   r5   r.  r\   r   unittestr   rf   r   r   r   r   r   r	   r
   psutil.testsr   r   r   r   r   r   r   r   r   r   r4  r3  psutil._psutil_posixr   r   r0   r8   r>   rB   rU   r   r   rY   r   r1  r   r1   r*   <module>r;     s0       				 				                                                                    , , , , , , # # # # # # ' ' ' ' ' '       ) ) ) ) ) )       . . . . . . ' ' ' ' ' ' " " " " " " 	 1KKKOOO000000( ( ( (n        8 8 8  Il33i& i& i& i& i&. i& i& 43i&X Il33]= ]= ]= ]= ]=^ ]= ]= 43]=@ Il33) ) ) ) )~ ) ) 43) ) )r1   