a }|gj@s4dZddlZddlZddlZddlZddlmZddlZddlmZddl m Z ddl m Z ddl m Z ddlmmZddlmZz ddlZddlZddlZd ZWneyd ZYn0d Zd Zd ZejeddGdddeZGdddeZGdddeZejeddGdddeZGdddejZejeddGdddejZ GdddejZ!Gdd d ejZ"Gd!d"d"ejZ#Gd#d$d$ejZ$Gd%d&d&ejZ%Gd'd(d(ejZ&Gd)d*d*ej'Z(ej)d+d,Z*d-d.Z+d/d0Z,d1d2Z-d8d4d5Z.e/d6kr0e0e1ej2d7de3gdS)9z#Tests for certbot.compat.filesystemN)mock)util)lock) filesystem)os)TempDirTestCaseFTzS-1-1-0zS-1-5-18z S-1-5-32-544z"Tests specific to Windows securityreasoncspeZdZdZfddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ ddZ ddZZS)WindowsChmodTestsz:Unit tests for Windows chmod function in filesystem modulecstt|j|_dSNsupersetUp _create_probetempdir probe_pathself __class__R/usr/lib/python3.9/site-packages/certbot/_internal/tests/compat/filesystem_test.pyr#s zWindowsChmodTests.setUpcCstj|jd}t|j|t|j}t|}t |dt|j}t|}t ||rnJt ||s~JdS)Nlink) rpathjoinrsymlinkr_get_security_daclGetSecurityDescriptorDaclrchmod_compare_dacls)r link_pathref_dacl_probeZ ref_dacl_linkcur_dacl_probeZ cur_dacl_linkrrrtest_symlink_resolution's   z)WindowsChmodTests.test_symlink_resolutioncsttt|jdt|jfddtd DrHJt|jdt|jfddtd DsJdS)Nrcs(g|] }|dkr|qSZGetAce.0indexdaclZ everybodyrr <sz;WindowsChmodTests.test_world_permission..rics(g|] }|dkr|qSr%r'r(r+rrr-Bs) win32securityConvertStringSidToSid EVERYBODY_SIDrrrrrrange GetAceCountrrr+rtest_world_permission6s "z'WindowsChmodTests.test_world_permissioncCsLt|jdt|j}t|jdt|j}t||sHJdS)Nri)rrrrrr )rr"r#rrrtest_group_permissions_noopEs z-WindowsChmodTests.test_group_permissions_noopcsttttt|jdt|jfddt d D}fddt d D}t |dks|Jt |dksJ|ddt j ksJ|ddt j ksJdS)Ncs(g|] }|dkr|qSr%r'r()r,systemrrr-Usz.rcs(g|] }|dkr|qSr%r'r()adminsr,rrr-Ws)r.r/ SYSTEM_SID ADMINS_SIDrrrrrr1r2len ntsecurityconFILE_ALL_ACCESS)rZ system_acesZ admin_acesr)r7r,r6rtest_admin_permissionsNs  z(WindowsChmodTests.test_admin_permissionscCs|dtjdS)N) _test_flagr<FILE_GENERIC_READrrrrtest_read_flag`sz WindowsChmodTests.test_read_flagcCs|dtjdSNr8)r@r<FILE_GENERIC_EXECUTErrrrtest_execute_flagcsz#WindowsChmodTests.test_execute_flagcCs|dtjtjAtjAdS)Nr&)r@r<r=rArDrrrrtest_write_flagfs  z!WindowsChmodTests.test_write_flagcCs|dtjdS)N)r@r<r=rrrrtest_full_flagksz WindowsChmodTests.test_full_flagcstt|jd|Bt|jttfddtd D}t |dksXJ|d}|d|kspJdS)Nrcs(g|] }|dkr|qSr%r'r(r+rrr-vsz0WindowsChmodTests._test_flag..rr8) rrrrrr.r/r0r1r2r;)rZ everyone_modeZ windows_flagZacls_everybodyrr+rr@ns zWindowsChmodTests._test_flagcCstdt\}}}t|j}t|j||t|jdt |j}| dksZJt t }t|j}t|j||t|jdt |j}| dksJdS)Nrr&)r.ZLookupAccountNamewin32apiZ GetUserName_get_security_ownerr _set_ownerrrrrr2r/r:)rZauthenticated_user_security_ownerZ security_daclZ admin_userrrr test_user_admin_dacl_consistencys     z2WindowsChmodTests.test_user_admin_dacl_consistency)__name__ __module__ __qualname____doc__rr$r3r4r>rBrErFrHr@rP __classcell__rrrrr s  r c@s*eZdZddZddZed ddZdS) UmaskTestc Cstd}ztj|jd}t|t|ddus:Jtdtj|jd}t|t|ddusrJtj|jd}tj|d d t|ddusJWt|n t|0dS) Nprobe1T?dir2rdir3)mode)rumaskrrrrmkdir check_mode)rprevious_umaskZdir1r[r\rrrtest_umask_on_dirs    zUmaskTest.test_umask_on_dirc Cstd}ztj|jd}t|t|ddus:Jtdtj|jd}t|t|ddusrJtj|jd}t|t|ddusJWt|n t|0dS) NrWrXrYTrZZprobe2rZprobe3) rr_rrrrrV _create_filera)rrbZfile1Zfile2Zfile3rrrtest_umask_on_files     zUmaskTest.test_umask_on_filer]c Cs>d}z$tj|tj|d}W|r:t|n|r8t|0dS)N)flagsr^)ropenrO_CREATclose)rr^Z file_descrrrrds zUmaskTest._create_fileN)r])rQrRrSrcre staticmethodrdrrrrrVsrVcs$eZdZfddZddZZS)ComputePrivateKeyModeTestcstt|j|_dSr r rrrrrs zComputePrivateKeyModeTest.setUpcCs>t|jdt|jd}tr.|dks:Jn |dks:JdS)Nr]ii)rrrZcompute_private_key_mode POSIX_MODE)rZnew_moderrrtest_compute_private_key_modes z7ComputePrivateKeyModeTest.test_compute_private_key_mode)rQrRrSrrmrUrrrrrks rkc@s,eZdZddZddZddZddZd S) WindowsOpenTestcsrtj|jd}t|tjtjBtjBd}t |t | t tfddtdDrnJdS)Nfilercs(g|] }|dkr|qSr%r'r(r+rrr-szEWindowsOpenTest.test_new_file_correct_permissions..r)rrrrrrgrhO_EXCLO_RDWRrirrr.r/r0r1r2rrZdescrr+r!test_new_file_correct_permissionss    z1WindowsOpenTest.test_new_file_correct_permissionscsztj|jd}t|dt|tjtjBd}t|t | t t fddtdDrvJdS)Nrowrcs(g|] }|dkr|qSr%r'r(r+rrr-szJWindowsOpenTest.test_existing_file_correct_permissions..r)rrrrrgrirrprqrrr.r/r0r1r2rrrr+r&test_existing_file_correct_permissionss   z6WindowsOpenTest.test_existing_file_correct_permissionscCsb|jddtjtjBdtt(}|jddtjtjBdWdn1sP0Y|jjtj kslJ|jddtjd|jddtjdtj |j d}t |d t|}z\tt"}|jd dtjdWdn1s0Y|jjtjks JW|n |0tt"|jd dtjdWdn1sT0YdS) Nr8F) file_existrfr&TrJr?5rt)_test_one_creationrrhrppytestraisesOSErrorvalueerrnoZEEXISTrrrrgrirZLockFileZEACCESreleaseO_RDONLY)rexc_inforZfilelockrrrtest_create_file_on_opens  6  0 z(WindowsOpenTest.test_create_file_on_openc Cstj|jt|}|rNtj|sNt|dWdn1sD0Yd}zt||}W|rt|n|rt|0dS)Nrt) rrrrstrexistsrgrri)rZnumrvrfZone_fileZhandlerrrrrzs  z"WindowsOpenTest._test_one_creationN)rQrRrSrsrurrzrrrrrns  rnc@s(eZdZdZddZddZddZdS) TempUmaskTestsz8Tests for using the TempUmask class in `with` statementscCstd}t||S)Nr)rr_)rZ old_umaskrrr _check_umask&s  zTempUmaskTests._check_umaskcCshtd|dksJtd |dks6JWdn1sJ0Y|dksdJdSNrWrZ)rr_r temp_umaskrrrrtest_works_normally+s   .z"TempUmaskTests.test_works_normallycCstd|dksJzDtd&|dks8JtWdn1sR0YWn|dksvJYn0dSr)rr_rr Exceptionrrrr!test_resets_umask_after_exception2s  (z0TempUmaskTests.test_resets_umask_after_exceptionN)rQrRrSrTrrrrrrrr$sr!Test specific to Windows securityc@s(eZdZdZddZddZddZdS) WindowsMkdirTestszFUnit tests for Windows mkdir + makedirs functions in filesystem modulecsXtj|jd}t|dttt | fddt d DrTJdS)Ndirrcs(g|] }|dkr|qSr%r'r(r+rrr-HszDWindowsMkdirTests.test_mkdir_correct_permissions..r) rrrrrr`r.r/r0rrr1r2)rrrr+rtest_mkdir_correct_permissions@s    z0WindowsMkdirTests.test_mkdir_correct_permissionscsftj|jd}tj|d}t|dttt | fddt d DrbJdS)Nrsubpathrcs(g|] }|dkr|qSr%r'r(r+rrr-TszGWindowsMkdirTests.test_makedirs_correct_permissions..r) rrrrrmakedirsr.r/r0rrr1r2)rrrrr+r!test_makedirs_correct_permissionsKs    z3WindowsMkdirTests.test_makedirs_correct_permissionscCsjtj|jd}ddl}|j}t|||jks6Jzt|WntyVYn0||jksfJdS)Nrr)rrrrr`rrr})rrstd_osZoriginal_mkdirrrrtest_makedirs_switch_os_mkdirWs  z/WindowsMkdirTests.test_makedirs_switch_os_mkdirN)rQrRrSrTrrrrrrrr=s  rc@seZdZdZddZdS) MakedirsTestsz5Unit tests for makedirs function in filesystem modulec Csttj|jd}tj|d}td}z:t|dt|dsFJt|dsVJWt|n t|0dS)NrrrWr)rrrrrr_rra)rrrrbrrrrhs  z/MakedirsTests.test_makedirs_correct_permissionsN)rQrRrSrTrrrrrrfsrcsjeZdZdZfddZejeddddZej eddd d Z d d Z ejeddd dZ Z S)CopyOwnershipAndModeTestzYTests about copy_ownership_and_apply_mode, copy_ownership_and_mode and has_same_ownershipcstt|j|_dSr r rrrrrys zCopyOwnershipAndModeTest.setUprrc s tt}tj}||dtdV}td,}||_t j d|j ddddWdn1sh0YWdn1s0Y|j dksJ|j d }|d d}||ksJ|j d }|d d}|ttsJfd d td DrJdS) NFwin32security.GetFileSecurityzwin32security.SetFileSecuritydummyrTZ copy_userZ copy_groupr&rr8cs(g|] }|dkr|qSr%r'r(r+rrr-szWCopyOwnershipAndModeTest.test_copy_ownership_and_apply_mode_windows..)r.r/r9SECURITY_ATTRIBUTESSECURITY_DESCRIPTORSetSecurityDescriptorOwnerrpatch return_valuercopy_ownership_and_apply_moderZ call_countZcall_args_listZGetSecurityDescriptorOwnerrr0r2r1)rr6securitymock_getZmock_setZ first_callZ second_callrr+r*test_copy_ownership_and_apply_mode_windows}s&      B      zCCopyOwnershipAndModeTest.test_copy_ownership_and_apply_mode_windowsTest specific to Linux securityc Cstd}td`}td6}d|j_d|j_tjd|jdddd Wdn1s^0YWdn1s|0YWdn1s0Y||jdd||jddS) Nzos.chownzos.chmodzos.stat23rrTr) rrrst_uidst_gidrrrassert_called_once_with)rZ mock_chownZ mock_chmodZ mock_statrrr(test_copy_ownership_and_apply_mode_linuxs    `zACopyOwnershipAndModeTest.test_copy_ownership_and_apply_mode_linuxcCsXtj|jd}tj|jd}t|dt|dt||dusTJdS)NZtest1Ztest2rtT) rrrrr safe_openrirZhas_same_ownership)rZpath1Zpath2rrrtest_has_same_ownerships z0CopyOwnershipAndModeTest.test_has_same_ownershipcCs|j}t|jdd}t|dt|ddus4Jt|ddusHJtd}t||Wdn1st0Y| ||t|ddusJdS)Ndst)namerTz-certbot.compat.filesystem._copy_win_ownership) rrrrrrarrZcopy_ownership_and_moder)rsrcrZmock_copy_ownerrrr$test_copy_ownership_and_mode_windowss  * z=CopyOwnershipAndModeTest.test_copy_ownership_and_mode_windows)rQrRrSrTrunittestskipIfrlr skipUnlessrrrrUrrrrrws      rcsleZdZdZfddZddZejedddd Z ej ed dd d Z d dZ ddZ ddZZS)CheckPermissionsTestz-Tests relative to functions that check modes.cstt|j|_dSr r rrrrrs zCheckPermissionsTest.setUpcCs:t|jddusJt|jdt|jdr6JdSNrTr)rrarrrrrrtest_check_modesz$CheckPermissionsTest.test_check_moderrcCsxt|jdusJtt}tj}||dt d&}||_ t|jrVJWdn1sj0YdS)NTFr) r check_ownerrr.r/r9rrrrrr)rr6rrrrrtest_check_owner_windowss    z-CheckPermissionsTest.test_check_owner_windowsrcCslt|jdusJddl}|}td*}|d|_t|jrJJWdn1s^0YdS)NTrz os.getuidr8)rrrrgetuidrrr)rruidZmock_uidrrrtest_check_owner_linuxs   z+CheckPermissionsTest.test_check_owner_linuxcCst|jddusJtd(}d|_t|jdr:JWdn1sN0Ytd(}d|_t|jdr|JWdn1s0YdS)NrTz$certbot.compat.filesystem.check_modeFz%certbot.compat.filesystem.check_owner)rZcheck_permissionsrrrr)rZ mock_modeZ mock_ownerrrrtest_check_permissionss 0 z+CheckPermissionsTest.test_check_permissionscCsht|jdt|jddus$Jt|jdt|jdrDJt|jdt|jdrdJdS)NrTri)rrrZhas_min_permissionsrrrrtest_check_min_permissionss z/CheckPermissionsTest.test_check_min_permissionscCsDt|jdt|jdus"Jt|jdt|jr@JdSr)rrrZhas_world_permissionsrrrrtest_is_world_reachablesz,CheckPermissionsTest.test_is_world_reachable)rQrRrSrTrrrrrlrrrrrrrUrrrrrs      rc@seZdZdZddZdS) OsReplaceTestz3Test to ensure consistent behavior of rename methodcCsptj|jd}tj|jd}t|dt|dt||tj|rXJtj|duslJdS)zKEnsure that replace will effectively rename src into dst for all platforms.rrrtTN) rrrrrgrirreplacer)rrrrrr test_os_replace_to_existing_files z.OsReplaceTest.test_os_replace_to_existing_fileN)rQrRrSrTrrrrrrsrcs0eZdZdZfddZddZddZZS) RealpathTestzTests for realpath methodcstt|j|_dSr r rrrrrs zRealpathTest.setUpc Cst|j|_tj|jd}t|j||jt|jksBJ|jt|ksVJt}tj|jd}tj |j}zVt tj |jt|||jt|ksJ|jt|ksJWt |n t |0dS)NZlink_absZlink_rel) rrealpathrrrrrrgetcwdbasenamechdirdirname)rr!curdirZ probe_namerrrr$s z$RealpathTest.test_symlink_resolutioncCstj|jd}tj|jd}tj|jd}t||t||t||tjtdd}t |Wdn1s0YdS)NZlink1Zlink2Zlink3zlink1 is a loop!)match) rrrrrr{r| RuntimeErrorrr)rZ link1_pathZ link2_pathZ link3_patherrorrrrtest_symlink_loop_mitigation0s   z)RealpathTest.test_symlink_loop_mitigation)rQrRrSrTrr$rrUrrrrrs rc@sleZdZdZddZededddZededdd Zededd d Z d S) IsExecutableTestzTests for is_executable methodcstj|jd}ddlmd fdd }tjd|d,tt |tj tj BdWdn1sj0Yt |rJdS) NZfoor_generate_daclcs.|||}td|D]}|dq|SrC)r1r2Z DeleteAce)Zuser_sidr^maskr,rNrrr _execute_mockKs  z;IsExecutableTest.test_not_executable.._execute_mockz(certbot.compat.filesystem._generate_dacl)Z side_effecti)N)rrrrZcertbot.compat.filesystemrrrrirrgrhO_WRONLY is_executable)rZ file_pathrrrrtest_not_executable>s  :z$IsExecutableTest.test_not_executablez(certbot.compat.filesystem.os.path.isfilez#certbot.compat.filesystem.os.accesscCsHt.d|_d|_tddus&JWdn1s:0YdS)NTz /path/to/exe_fix_windows_runtimerrrrZ mock_accessZ mock_isfilerrrtest_full_pathWszIsExecutableTest.test_full_pathcCsHt.d|_d|_tddus&JWdn1s:0YdS)NTexerrrrr test_rel_path_szIsExecutableTest.test_rel_pathcCsDt*d|_d|_tdr"JWdn1s60YdS)NTFrrrrrrtest_not_foundgszIsExecutableTest.test_not_foundN) rQrRrSrTrrrrrrrrrrr<s  rc@sleZdZejeddedddZej eddedddZ ej eddedd d Z d S) ReadlinkTestzTests specific to Linuxrz%certbot.compat.filesystem.os.readlinkcCsd|_tddksJdS)Nz /normal/pathrrrreadlinkrZ mock_readlinkrrrtest_path_posixqszReadlinkTest.test_path_posixzTests specific to WindowscCsd|_tddksJdS)Nz\\?\C:\short\pathrz C:\short\pathrrrrrtest_normal_path_windowswsz%ReadlinkTest.test_normal_path_windowscCsFddd|_tttdWdn1s80YdS)Nz \\?\C:\longiz\pathr)rr{r| ValueErrorrrrrrrtest_extended_path_windows~s z'ReadlinkTest.test_extended_path_windowsN) rQrRrSrrrlrrrrrrrrrrrps     rccs^tjdkrdVnHtd.}|jj}|jj}tj|_dVWdn1sP0YdS)Nntr) rrrrrrZGetEffectiveRightsFromAclr<rD)rZ dacl_mockZ mode_mockrrrrs  rcCst|tjSr )r.GetFileSecurityZDACL_SECURITY_INFORMATIONtargetrrrrsrcCst|tjSr )r.rOWNER_SECURITY_INFORMATIONrrrrrLsrLcCs ||dt|tj|dS)NF)rr.ZSetFileSecurityr)rrOuserrrrrMs rMprobecCs2t|dtj||}tj|ddd|S)Nrrt)r)rrrrrrrri)rrrrrrrs r__main__r8)r)4rT contextlibrsysrrr{ZcertbotrZcertbot._internalrZcertbot.compatrrZcertbot.tests.utilZtestsZ test_utilrr<rKr.rl ImportErrorr0r9r:rr rVrkrnrrrrrrrrZTestCasercontextmanagerrrrLrMrrQexitmainargv__file__rrrrs\         w3 F (IC)4