
    g D                        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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 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)AIX)BSD)LINUX)MACOS)OPENBSD)POSIX)SUNOS)AARCH64)HAS_NET_IO_COUNTERS)
PYTHON_EXE)	QEMU_USER)PsutilTestCase)mock)pytest)retry_on_failure)sh)skip_on_access_denied)spawn_testproc)	terminate)which)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         S/var/www/html/ai-engine/env/lib/python3.11/site-packages/psutil/tests/test_posix.pyr   r   /   sm   
 &C #

>"""


D#c((#$$$$ 	C 	JJtJJt ,kk'7++JJc{WWF$)FV   v/@/@/B/B122/FFJ    zz||	t99DD 	 	 	D	 	$
{!}s   D%%
D21D2c                     d}t           rd}t          ||                                           }t          rd|d         v sJ |d         S |d         S )Ncommandcommz
/bin/qemu-r   r   )r	   r   splitr   )r'   fieldr.   s      r,   ps_namer2   g   s^    E nn""$$G wqz))))qz1:    c                     d}t           st          rd}t          ||           }t          j        dd|          }|                                S )Nr.   argsz\(python.*?\)$ )r   r	   r   resubr#   )r'   r1   outs      r,   ps_argsr:   r   sJ    E
 e 
UC..C
&"B
,
,C99;;r3   c                 8    d}t           rd}t          ||           S )Nrssrssizer   r   r'   r1   s     r,   ps_rssr@   |   s"    E
 eS>>r3   c                 8    d}t           rd}t          ||           S )Nvszvsizer>   r?   s     r,   ps_vszrD      s"    E
 eS>>r3   c                 .   	 t          d| z                                            }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 %szdevice busyzdf returned EBUSY
r               %r6   )r   r#   RuntimeErrorr    lowerr   skipr0   r$   floatr!   )	devicer9   errr+   fields	sys_totalsys_usedsys_freesys_percents	            r,   dfrW      s   f$%%++--   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
 
 

 	r3   c                 .    t          | j                   d S N)r   r'   r`   s    r,   tearDownClasszTestProcess.tearDownClass   s    #'r3   c                     t          d| j                  }t          j        | j                                                  }||k    sJ d S )Nppid)r   r'   psutilProcessrg   )selfppid_psppid_psutils      r,   	test_ppidzTestProcess.test_ppid   sG    VTX&&nTX..3355+%%%%%%r3   c                     t          d| j                  }t          j        | j                                                  j        }||k    sJ d S )Nuid)r   r'   rh   ri   uidsreal)rj   uid_ps
uid_psutils      r,   test_uidzTestProcess.test_uid   sJ    E48$$^DH--22449
######r3   c                     t          d| j                  }t          j        | j                                                  j        }||k    sJ d S )Nrgid)r   r'   rh   ri   gidsrq   )rj   gid_ps
gid_psutils      r,   test_gidzTestProcess.test_gid   sJ    FDH%%^DH--22449
######r3   c                     t          d| j                  }t          j        | j                                                  }||k    sJ d S )Nuser)r   r'   rh   ri   username)rj   username_psusername_psutils      r,   test_usernamezTestProcess.test_username   sG    ** .22;;==o------r3   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)
rh   ri   r   patchKeyErrorr}   r    rp   rq   called)rj   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   rG   )timesleepr@   r'   rh   ri   memory_info)rj   rss_ps
rss_psutils      r,   test_rss_memoryzTestProcess.test_rss_memory   ^    
 	
3!!^DH--99;;A>E
######r3   c                     t          j        d           t          | j                  }t	          j        | j                                                  d         dz  }||k    sJ d S )Nr   r   rG   )r   r   rD   r'   rh   ri   r   )rj   vsz_ps
vsz_psutils      r,   test_vsz_memoryzTestProcess.test_vsz_memory   r   r3   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.\dr6   z\d)r2   r'   ospathbasenamerM   rh   ri   namer7   r8   )rj   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+%%%%%%r3   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   rh   ri   r   )rj   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   r6   r   )r   r   rh   AccessDeniedri   r   rj   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   rh   NoSuchProcessri   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 availablerX   c                 |   t          d| j                  }t          j        | j                                                  }t
          j                            |                              d          }t          |          }t
          j                            |                              d          }|||fv sJ d S )Nr   z%H:%M:%S)	r   r'   rh   ri   create_timedatetimefromtimestampstrftimeround)rj   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Hr3   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rd   )r2   r'   rh   ri   exeAssertionErrorlen)rj   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 )	r:   r'   joinrh   ri   r   r
   r   
startswith)rj   
ps_cmdlinepsutil_cmdlines      r,   test_cmdlinezTestProcess.test_cmdline1  s    TX&&
&.":":"B"B"D"DEE 	0s:^)<)<<<!,,Z8888888//////r3   znot reliable on SUNOSznot reliable on AIXc                     t          d| j                  }t          j                                                    }||k    sJ d S )Nnice)r   r'   rh   ri   r   )rj   ps_nicepsutil_nices      r,   	test_nicezTestProcess.test_nice?  sE     VTX&&n&&++--+%%%%%%r3   N)__name__
__module____qualname____doc__classmethodrb   re   rm   rt   rz   r   r   r   r   r   r   r   r   r   r   r   markskipifr   r   r   r   r   r	   r   r    r3   r,   r[   r[      s       LL  [
   [& & &
$ $ $
$ $ $
. . .
   $ $  $ $ $  $& & &@ @ @7 7 7   [-HIII I JII7 7 7( 0 0 0 [&=>>[$9::& & ;: ?>& & &r3   r[   c                      e Zd ZdZ e            d             Zej                            e	d          ej                             e
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>X  s#    EEEAW4D4D!4D4D4Dr3   c                     g | ]}|v|	S r   r   )r   r   pids_psutils     r,   r   z,TestSystemAPIs.test_pids.<locals>.<listcomp>X  s-     I I Ia{&:&:&:&:&:r3   zdifference: )
sortedr   rh   pidsr   r   insertr   failr    )rj   
differencer   r   s     @@r,   	test_pidszTestSystemAPIs.test_pidsK  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__<===	 /.r3   zunreliable on SUNOSrX   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   rh   net_io_countersr0   r   r   )rj   r)   nicr+   s       r,   test_nic_nameszTestSystemAPIs.test_nic_names_  s     M"")666 	 	C  ??3'' E iiissFF$   	 	r3   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 systemrF   c                 B    g | ]}|                                 d          S )r   r0   r   r   s     r,   r   z-TestSystemAPIs.test_users.<locals>.<listcomp>v  s$    ---!1---r3   c                 B    g | ]}|                                 d          S )r   r   r   s     r,   r   z-TestSystemAPIs.test_users.<locals>.<listcomp>w  s$    111aQWWYYq\111r3   rh   who)r   r#   r   rN   r0   r   rh   userssubTest	enumerater   terminalr'   ri   )rj   r9   linesr   	terminalsidxus          r,   
test_userszTestSystemAPIs.test_usersp  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|z            |                     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"Cr3   z(cannot interpret tstamp in who output
%sr   )r   r#   r   rN   r7   findallr   rh   r   r   r   r   startedr   )rj   r9   tstampr  r   r  psutil_values          r,   test_users_startedz!TestSystemAPIs.test_users_started  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 	+;sC   \\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.killr6   r   )r   r   OSErrorerrnoEBADFr   r   rh   _psposix
pid_existsr   getpidr   rj   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.waitpidr6   r   )r   r   r  r  r  r   r   rh   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  r6   r   g{Gz?)timeout)r   r   r  r  EINTRr   r   rh   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%   rh   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)rh   disk_partitions
disk_usage
mountpointrW   rP   abstotalusedfreepercentrL   r    rM   )	rj   	tolerancepartusagerS   rT   rU   rV   rQ   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	   r   r   r   r  r
  r  r  r  r!  r   r/  r   r3   r,   r   r   G  s         > > >& [&;<<[EE*---6GHH[//HH
 
 IH IH =<
 * * * $4 $4 $4L	 	 	       [$788= =  98= = =r3   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)rj   pagesizes     r,   test_getpagesizezTestMisc.test_getpagesize  sK    ==!||||8/1111114=((((((r3   N)r   r   r   r7  r   r3   r,   r1  r1    s#        ) ) ) ) )r3   r1  rd   ),r   r   r  r   r7   r^   r   rh   r   r   r   r   r   r   r	   psutil.testsr
   r   r   r   r   r   r   r   r   r   r   r   r   r4  r3  psutil._psutil_posixr   r   r2   r:   r@   rD   rW   r   r   r[   r   r1  r   r3   r,   <module>r:     sK       				 				                                                             , , , , , , # # # # # # " " " " " " ' ' ' ' ' '             ) ) ) ) ) )       . . . . . . ' ' ' ' ' ' " " " " " "       	 1KKKOOO000000) ) ) )p        8 8 8  Il33i& i& i& i& i&. i& i& 43i&X Il33_= _= _= _= _=^ _= _= 43_=D Il33) ) ) ) )~ ) ) 43) ) )r3   