Your IP : 216.73.216.172


Current Path : /lib/python3/dist-packages/certbot/tests/compat/__pycache__/
Upload File :
Current File : //lib/python3/dist-packages/certbot/tests/compat/__pycache__/filesystem_test.cpython-38.pyc

U

�]�W�@s�dZddlZddlZddlZddlZz ddlZddlZddlZdZWne	k
r\dZYnXddl
mmZ
ddlmZddlmZddlmZddlmZdd	l
mZd
ZdZdZejed
d�Gdd�de��ZGdd�de�Zejed
d�Gdd�de��Zejedd�Gdd�de
j��ZGdd�de
j�ZGdd�de
j�ZGdd�de
j�ZGdd�de
j�ZGd d!�d!e
j�Z ej!d"d#��Z"d$d%�Z#d&d'�Z$d(d)�Z%d*d+�Z&e'd,k�r�e�(�dS)-z#Tests for certbot.compat.filesystem�NFT)�lock)�util)�os)�
filesystem)�TempDirTestCasezS-1-1-0zS-1-5-18zS-1-5-32-544z"Tests specific to Windows security��reasoncspeZdZdZ�fdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Z�ZS)�WindowsChmodTestsz:Unit tests for Windows chmod function in filesystem modulecstt|���t|j�|_dS�N)�superr	�setUp�
_create_probe�tempdir�
probe_path��self��	__class__��F/usr/lib/python3/dist-packages/certbot/tests/compat/filesystem_test.pyr"szWindowsChmodTests.setUpcCs�tj�|jd�}t�|j|�t|j���}t|���}t�	|d�t|j���}t|���}|�
t�||��|�t�||��dS)N�link�)
r�path�joinr�symlinkr�_get_security_dacl�GetSecurityDescriptorDaclr�chmod�assertFalse�_compare_dacls�
assertTrue)r�	link_path�ref_dacl_probeZ
ref_dacl_link�cur_dacl_probeZ
cur_dacl_linkrrr�test_symlink_resolution&sz)WindowsChmodTests.test_symlink_resolutioncs�t�t��t�|jd�t|j����|���fdd�t	d��
��D��t�|jd�t|j����|���fdd�t	d��
��D��dS)Nrcs(g|] }��|�d�kr��|��qS���ZGetAce��.0�index��daclZ	everybodyrr�
<listcomp>;s�z;WindowsChmodTests.test_world_permission.<locals>.<listcomp>ri�cs(g|] }��|�d�kr��|��qSr%r'r(r+rrr-As�)�
win32security�ConvertStringSidToSid�
EVERYBODY_SIDrrrrrr�range�GetAceCountr rrr+r�test_world_permission5s
$z'WindowsChmodTests.test_world_permissioncCsNt�|jd�t|j���}t�|jd�t|j���}|�t�||��dS)Nri�)rrrrrr r)rr"r#rrr�test_group_permissions_noopDs
z-WindowsChmodTests.test_group_permissions_noopcs�t�t��t�t��t�|jd�t|j������fdd�t	d��
��D�}��fdd�t	d��
��D�}|�t|�d�|�t|�d�|�|ddt
j�|�|ddt
j�dS)N�cs(g|] }��|�d�kr��|��qSr%r'r()r,�systemrrr-Ts�z<WindowsChmodTests.test_admin_permissions.<locals>.<listcomp>rcs(g|] }��|�d�kr��|��qSr%r'r()�adminsr,rrr-Vs��)r.r/�
SYSTEM_SID�
ADMINS_SIDrrrrrr1r2�assertEqual�len�
ntsecuritycon�FILE_ALL_ACCESS)rZsystem_acesZ
admin_acesr)r7r,r6r�test_admin_permissionsMs

z(WindowsChmodTests.test_admin_permissionscCs|�dtj�dS)N�)�
_test_flagr=�FILE_GENERIC_READrrrr�test_read_flag_sz WindowsChmodTests.test_read_flagcCs|�dtj�dS�Nr8)rAr=�FILE_GENERIC_EXECUTErrrr�test_execute_flagbsz#WindowsChmodTests.test_execute_flagcCs|�dtjtjAtjA�dS)Nr&)rAr=r>rBrErrrr�test_write_flages

��z!WindowsChmodTests.test_write_flagcCs|�dtj�dS)N�)rAr=r>rrrr�test_full_flagjsz WindowsChmodTests.test_full_flagcstt�|jd|B�t|j����t�t����fdd�td��	��D�}|�
t|�d�|d}|�
|d|�dS)Nrcs(g|] }��|�d�kr��|��qSr%r'r(r+rrr-us�z0WindowsChmodTests._test_flag.<locals>.<listcomp>rr8)rrrrrr.r/r0r1r2r;r<)rZ
everyone_modeZwindows_flagZacls_everybodyrr+rrAms
zWindowsChmodTests._test_flagcCs�t�dt���\}}}t|j�}t|j||�t�|jd�t	|j�}|�
|����d�t�
t�}t|j�}t|j||�t�|jd�t	|j�}|�
|����d�dS)N�r�r&)r.ZLookupAccountName�win32apiZGetUserName�_get_security_ownerr�
_set_ownerrrrr;rr2r/r:)rZauthenticated_user�_�security_ownerZ
security_daclZ
admin_userrrr� test_user_admin_dacl_consistency~s




z2WindowsChmodTests.test_user_admin_dacl_consistency)�__name__�
__module__�__qualname__�__doc__rr$r3r4r?rCrFrGrIrArQ�
__classcell__rrrrr	s	r	cs$eZdZ�fdd�Zdd�Z�ZS)�ComputePrivateKeyModeTestcstt|���t|j�|_dSr
)rrWrr
rrrrrrr�szComputePrivateKeyModeTest.setUpcCs>t�|jd�t�|jd�}tr.|�|d�n|�|d�dS)Ni�i�i�)rrrZcompute_private_key_mode�
POSIX_MODEr;)rZnew_moderrr�test_compute_private_key_mode�s
z7ComputePrivateKeyModeTest.test_compute_private_key_mode)rRrSrTrrYrVrrrrrW�srWc@s,eZdZdd�Zdd�Zdd�Zdd�Zd	S)
�WindowsOpenTestcsttj�|jd�}t�|tjtjBtjBd�}t�	|�t
|����t�
t��|���fdd�td����D��dS)N�filercs(g|] }��|�d�kr��|��qSr%r'r(r+rrr-�s�zEWindowsOpenTest.test_new_file_correct_permissions.<locals>.<listcomp>r)rrrrr�open�O_CREAT�O_EXCL�O_RDWR�closerrr.r/r0rr1r2�rrZdescrr+r�!test_new_file_correct_permissions�s

z1WindowsOpenTest.test_new_file_correct_permissionscs|tj�|jd�}t|d���t�|tjtjBd�}t�|�t	|��
��t�t
��|���fdd�td����D��dS)Nr[�wrcs(g|] }��|�d�kr��|��qSr%r'r(r+rrr-�s�zJWindowsOpenTest.test_existing_file_correct_permissions.<locals>.<listcomp>r)rrrrr\r`rr^r_rrr.r/r0rr1r2rarr+r�&test_existing_file_correct_permissions�s

z6WindowsOpenTest.test_existing_file_correct_permissionsc	Cs|jddtjtjBd�|�t��}|jddtjtjBd�W5QRX|�|jjtj	�|jddtjd�|jddtjd�tj
�|jd�}t
|d	���t�|�}z>|�t��}|jd
dtjd�W5QRX|�|jjtj�W5|��X|�t��|jddtjd�W5QRXdS)Nr8F)�
file_exist�flagsr&TrKr@�5rc��)�_test_one_creationrr]r^�assertRaises�OSErrorr;�	exception�errnoZEEXISTrrrr\r`rZLockFile�releaseZEACCES�O_RDONLY)rZraisedrZfilelockrrr�test_create_file_on_open�s "

z(WindowsOpenTest.test_create_file_on_openc	Csdtj�|jt|��}|r:tj�|�s:t|d��W5QRXd}zt�||�}W5|r^t�|�XdS)Nrc)	rrrr�str�existsr\r`r)rZnumrerfZone_fileZhandlerrrrrj�s
z"WindowsOpenTest._test_one_creationN)rRrSrTrbrdrqrjrrrrrZ�s
rZ�!Test specific to Windows securityc@s(eZdZdZdd�Zdd�Zdd�ZdS)	�WindowsMkdirTestszFUnit tests for Windows mkdir + makedirs functions in filesystem modulecsZtj�|jd�}t�|d�t�t��t	|��
��|���fdd�td��
��D��dS)N�dirrcs(g|] }��|�d�kr��|��qSr%r'r(r+rrr-�s�zDWindowsMkdirTests.test_mkdir_correct_permissions.<locals>.<listcomp>r)rrrrr�mkdirr.r/r0rrrr1r2)rrrr+r�test_mkdir_correct_permissions�s

z0WindowsMkdirTests.test_mkdir_correct_permissionscshtj�|jd�}tj�|d�}t�|d�t�t��t	|��
��|���fdd�td��
��D��dS)Nrv�subpathrcs(g|] }��|�d�kr��|��qSr%r'r(r+rrr-s�zGWindowsMkdirTests.test_makedirs_correct_permissions.<locals>.<listcomp>r)rrrrr�makedirsr.r/r0rrrr1r2)rrryrr+r�!test_makedirs_correct_permissions�s
z3WindowsMkdirTests.test_makedirs_correct_permissionscCsltj�|jd�}ddl}|j}t�|�|�||j�zt�|�Wntk
rXYnX|�||j�dS)Nrvr)	rrrrrwrrzr;rl)rr�std_osZoriginal_mkdirrrr�test_makedirs_switch_os_mkdir
s
z/WindowsMkdirTests.test_makedirs_switch_os_mkdirN)rRrSrTrUrxr{r}rrrrru�srucsTeZdZdZ�fdd�Zejedd�dd��Zej	edd�d	d
��Z
dd�Z�ZS)
�
OwnershipTestz@Tests about copy_ownership_and_apply_mode and has_same_ownershipcstt|���t|j�|_dSr
)rr~rr
rrrrrrrszOwnershipTest.setUprtrc
s�t�t�}t��j}|�|d�t�d��8}t�d��"}||_t	j
d|jdddd�W5QRXW5QRX|�|j
d�|jd	}|d	d}|�||���|jd
}|d	d}|���t�t��|�����|���fdd�td	����D��dS)
NF�win32security.GetFileSecurityzwin32security.SetFileSecurity�dummyrT�Z	copy_userZ
copy_groupr&rr8cs(g|] }��|�d�kr��|��qSr%r'r(r+rrr-6s�z=OwnershipTest.test_copy_ownership_windows.<locals>.<listcomp>)r.r/r9�SECURITY_ATTRIBUTES�SECURITY_DESCRIPTOR�SetSecurityDescriptorOwner�mock�patch�return_valuer�copy_ownership_and_apply_moderr;Z
call_countZcall_args_listZGetSecurityDescriptorOwnerrr0r r2rr1)rr6�security�mock_getZmock_setZ
first_callZsecond_callrr+r�test_copy_ownership_windowss.

�


z)OwnershipTest.test_copy_ownership_windows�Test specific to Linux securitycCs�t�d��X}t�d��B}t�d��,}d|j_d|j_tjd|jdddd	�W5QRXW5QRXW5QRX|�|jdd�|�|jd�dS)
Nzos.chownzos.chmodzos.stat�2�3r�rTr�)	r�r�r��st_uid�st_gidrr�rZassert_called_once_with)rZ
mock_chownZ
mock_chmodZ	mock_statrrr�test_copy_ownership_linux9s�$z'OwnershipTest.test_copy_ownership_linuxcCsVtj�|jd�}tj�|jd�}t�|d���t�|d���|�t�	||��dS)NZtest1Ztest2rc)
rrrrr�	safe_openr`r rZhas_same_ownership)rZpath1Zpath2rrr�test_has_same_ownershipFs
z%OwnershipTest.test_has_same_ownership)
rRrSrTrUr�unittest�skipIfrXr��
skipUnlessr�r�rVrrrrr~s

r~csleZdZdZ�fdd�Zdd�Zejedd�dd	��Z	ej
ed
d�dd��Zd
d�Zdd�Z
dd�Z�ZS)�CheckPermissionsTestz-Tests relative to functions that check modes.cstt|���t|j�|_dSr
)rr�rr
rrrrrrrRszCheckPermissionsTest.setUpcCs:|�t�|jd��t�|jd�|�t�|jd��dS�N��r)r rZ
check_moderrrrrrr�test_check_modeVsz$CheckPermissionsTest.test_check_modertrc	Csd|�t�|j��t�t�}t��j}|�	|d�t
�d��}||_|�
t�|j��W5QRXdS)NFr)r r�check_ownerrr.r/r9r�r�r�r�r�r�r)rr6r�r�rrr�test_check_owner_windows\s

z-CheckPermissionsTest.test_check_owner_windowsr�c	CsX|�t�|j��ddl}|��}t�d��"}|d|_|�	t�|j��W5QRXdS)Nrz	os.getuidr8)
r rr�rr�getuidr�r�r�r)rr|ZuidZmock_uidrrr�test_check_owner_linuxhs
z+CheckPermissionsTest.test_check_owner_linuxc	Csx|�t�|jd��t�d�� }d|_|�t�|jd��W5QRXt�d�� }d|_|�t�|jd��W5QRXdS)Nr�z$certbot.compat.filesystem.check_modeFz%certbot.compat.filesystem.check_owner)r rZcheck_permissionsrr�r�r�r)rZ	mock_modeZ
mock_ownerrrr�test_check_permissionsssz+CheckPermissionsTest.test_check_permissionscCsjt�|jd�|�t�|jd��t�|jd�|�t�|jd��t�|jd�|�t�|jd��dS)Nr�ri�)rrrr Zhas_min_permissionsrrrrr�test_check_min_permissions~sz/CheckPermissionsTest.test_check_min_permissionscCsDt�|jd�|�t�|j��t�|jd�|�t�|j��dSr�)rrrr Zhas_world_permissionsrrrrr�test_is_world_reachable�sz,CheckPermissionsTest.test_is_world_reachable)rRrSrTrUrr�r�r�rXr�r�r�r�r�r�rVrrrrr�Ps



r�c@seZdZdZdd�ZdS)�
OsReplaceTestz3Test to ensure consistent behavior of rename methodcCsptj�|jd�}tj�|jd�}t|d���t|d���t�||�|�tj�	|��|�
tj�	|��dS)zKEnsure that replace will effectively rename src into dst for all platforms.�src�dstrcN)rrrrr\r`r�replacerrsr )rr�r�rrr� test_os_replace_to_existing_file�sz.OsReplaceTest.test_os_replace_to_existing_fileN)rRrSrTrUr�rrrrr��sr�cs0eZdZdZ�fdd�Zdd�Zdd�Z�ZS)�RealpathTestzTests for realpath methodcstt|���t|j�|_dSr
)rr�rr
rrrrrrr�szRealpathTest.setUpc	Cs�t�|j�|_tj�|jd�}t�|j|�|�|jt�|j��|�|jt�|��t�	�}tj�|jd�}tj�
|j�}zLt�tj�|j��t�||�|�|jt�|��|�|jt�|��W5t�|�XdS)NZlink_absZlink_rel)
r�realpathrrrrrrr;�getcwd�basename�chdir�dirname)rr!�curdirZ
probe_namerrrr$�sz$RealpathTest.test_symlink_resolutionc	Cs�tj�|jd�}tj�|jd�}tj�|jd�}t�||�t�||�t�||�|�t��}t�|�W5QRX|�	dt
|j�k�dS)NZlink1Zlink2Zlink3zlink1 is a loop!)rrrrrrk�RuntimeErrorrr�r rrrm)rZ
link1_pathZ
link2_pathZ
link3_path�errorrrr�test_symlink_loop_mitigation�sz)RealpathTest.test_symlink_loop_mitigation)rRrSrTrUrr$r�rVrrrrr��sr�c@sleZdZdZdd�Ze�d�e�d�dd���Ze�d�e�d�dd	���Ze�d�e�d�d
d���Z	dS)
�IsExecutableTestzTests for is_executable methodc	srtj�|jd�}ddlm��fdd�}tjd|d��"t�t	�
|tjtjBd��W5QRX|�
t	�|��dS)	NZfoor��_generate_daclcs,�||�}td|���D]}|�d�q|SrD)r1r2Z	DeleteAce)Zuser_sid�moder,rOr�rr�
_execute_mock�s
z;IsExecutableTest.test_not_executable.<locals>._execute_mockz(certbot.compat.filesystem._generate_dacl)Zside_effecti�)rrrrZcertbot.compat.filesystemr�r�r�r`rr\r]�O_WRONLYr�
is_executable)rZ	file_pathr�rr�r�test_not_executable�s
&z$IsExecutableTest.test_not_executablez(certbot.compat.filesystem.os.path.isfilez#certbot.compat.filesystem.os.accessc	Cs2t��"d|_d|_|�t�d��W5QRXdS)NTz/path/to/exe��_fix_windows_runtimer�r rr��rZmock_accessZmock_isfilerrr�test_full_path�szIsExecutableTest.test_full_pathc	Cs2t��"d|_d|_|�t�d��W5QRXdS)NT�exer�r�rrr�
test_rel_path�szIsExecutableTest.test_rel_pathc	Cs2t��"d|_d|_|�t�d��W5QRXdS)NTFr�)r�r�rrr�r�rrr�test_not_found�szIsExecutableTest.test_not_foundN)
rRrSrTrUr�r�r�r�r�r�rrrrr��sr�c	csJtjdkrdVn4t�d��$}|jj}|jj}tj|_dVW5QRXdS)N�ntr)	r�namer�r�r�rZGetEffectiveRightsFromAclr=rE)r�Z	dacl_mockZ	mode_mockrrrr��s
r�cCst�|tj�Sr
)r.�GetFileSecurityZDACL_SECURITY_INFORMATION��targetrrrr
srcCst�|tj�Sr
)r.r��OWNER_SECURITY_INFORMATIONr�rrrrMsrMcCs |�|d�t�|tj|�dS)NF)r�r.ZSetFileSecurityr�)r�rP�userrrrrNs�rNcCs2t�|d�tj�|d�}tj|ddd���|S)Nr�Zproberc)r)rrrrrrr�r`)rrrrrr
sr
�__main__))rU�
contextlibrnr�r�rLr.r=rX�ImportErrorZcertbot.tests.utilZtestsrZ	test_utilZcertbotrZcertbot.compatrrrr0r9r:r�r	rWrZrur~r�r�r�r��contextmanagerr�rrMrNr
rR�mainrrrr�<module>sP
wF(7@*4