U
    !ºÖhý  ã                   @   s   d 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
„ Zdd„ ZG dd„ deƒZdS )zÃTests which are meant to be run as root.

NOTE: keep this module compatible with unittest: we want to run this
file with the unittest runner, since pytest may not be installed for
the root user.
é    N)ÚFREEBSD)ÚLINUX)ÚOPENBSD)ÚWINDOWS)Ú
CI_TESTING)ÚPsutilTestCasec                   C   s(   t tdƒr t tdƒr t tj¡S t ¡ S )NÚclock_gettimeÚCLOCK_REALTIME)ÚhasattrÚtimer   r	   © r   r   ú<./venv/lib/python3.8/site-packages/psutil/tests/test_sudo.pyÚget_systime   s    r   c                 C   sô   t tdƒrHt tdƒrHzt tj| ¡ W qð tk
rD   t d¡‚Y qðX n¨trædd l}dd l	}t
j
 | t
jj¡}z:| |j|j| ¡ d |j|j|j|jt|jd ƒ¡ W qð |jk
râ } z|jdkrÐt d¡‚‚ W 5 d }~X Y qðX n
t d	¡‚d S )
NÚclock_settimer	   z
needs rootr   é   iè  i"  zneeds Administrator userzsetting systime not supported)r
   r   r   r	   ÚPermissionErrorÚunittestZSkipTestr   Ú
pywintypesÚwin32apiÚdatetimeZfromtimestampÚtimezoneZutcZSetSystemTimeZyearZmonthZ
isoweekdayZdayZhourZminuteÚsecondÚintZmicrosecondÚerrorÚwinerror)Zsecsr   r   ZdtÚerrr   r   r   Úset_systime!   s2    
ø


r   c                   @   s†   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Ze 	e
d
¡dd„ ƒZe 	ed¡e 	ed¡e 	ed¡dd„ ƒƒƒZe 	e d¡dd„ ƒZdS )ÚTestUpdatedSystemTimez$Tests which update the system clock.c                 C   s   d| _ tƒ | _t ¡ | _d S )NF)Útime_updatedr   Ú	orig_timer   Ú	monotonicÚtime_started©Úselfr   r   r   ÚsetUpB   s    zTestUpdatedSystemTime.setUpc                 C   s&   | j r"t ¡ | j }t| j| ƒ d S ©N)r   r   r    r!   r   r   )r#   Zextra_tr   r   r   ÚtearDownG   s    zTestUpdatedSystemTime.tearDownc                 C   s   t | jd ƒ d| _d S )Né  T)r   r   r   r"   r   r   r   Úupdate_systimeL   s    z$TestUpdatedSystemTime.update_systimec                 C   sD   t  ¡ }|  ¡  t  ¡ }|  ||¡ t|| ƒ}| j|ddd d S ©Nr'   é   )Zdelta)ÚpsutilZ	boot_timer(   ZassertGreaterr   ÚassertAlmostEqual©r#   Út1Út2Zdiffr   r   r   Útest_boot_timeQ   s    z$TestUpdatedSystemTime.test_boot_timezbroken on WINDOWSc                 C   s@   t  ¡  ¡ }|  ¡  t  ¡  ¡ }t|| ƒ}| j|ddd d S r)   )r+   ÚProcessÚcreate_timer(   r   r,   r-   r   r   r   Útest_proc_create_timeZ   s
    z+TestUpdatedSystemTime.test_proc_create_timezskipped on CI for nowzbroken on OPENBSDzbroken on FREEBSDc                 C   s<   t  ¡ }|  ¡  t  ¡ }|  | ¡ | ¡ ¡ |  ||¡ d S r%   )r+   r1   r(   ÚassertEqualZ
_get_ident)r#   Zp1Zp2r   r   r   Útest_proc_idente   s
    z%TestUpdatedSystemTime.test_proc_identz
LINUX onlyc                 C   sF   t  ¡ jjdd}|  ¡  t d¡ t  ¡ jjdd}|  ||¡ d S )NT)r    gš™™™™™©?)r+   r1   Ú_procr2   r(   r   Úsleepr4   )r#   r.   r/   r   r   r   Útest_linux_monotonic_proc_timeo   s
    
z4TestUpdatedSystemTime.test_linux_monotonic_proc_timeN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r$   r&   r(   r0   r   ZskipIfr   r3   r   r   r   r5   r   r8   r   r   r   r   r   ?   s   	





r   )r<   r   r   r   r+   r   r   r   r   Zpsutil.testsr   r   r   r   r   r   r   r   r   Ú<module>   s   