Skip to content

gh-113086: Add tests for os.chmod() and os.lchmod() #113087

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 116 additions & 0 deletions Lib/test/test_posix.py
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,122 @@ def test_utime(self):
posix.utime(os_helper.TESTFN, (int(now), int(now)))
posix.utime(os_helper.TESTFN, (now, now))

def check_chmod(self, chmod_func, target, **kwargs):
mode = os.stat(target).st_mode
try:
new_mode = mode & ~(stat.S_IWOTH | stat.S_IWGRP | stat.S_IWUSR)
chmod_func(target, new_mode, **kwargs)
self.assertEqual(os.stat(target).st_mode, new_mode)
if stat.S_ISREG(mode):
try:
with open(target, 'wb+'):
pass
except PermissionError:
pass
new_mode = mode | (stat.S_IWOTH | stat.S_IWGRP | stat.S_IWUSR)
chmod_func(target, new_mode, **kwargs)
self.assertEqual(os.stat(target).st_mode, new_mode)
if stat.S_ISREG(mode):
with open(target, 'wb+'):
pass
finally:
posix.chmod(target, mode)

def test_chmod_file(self):
self.check_chmod(posix.chmod, os_helper.TESTFN)

def tempdir(self):
target = os_helper.TESTFN + 'd'
posix.mkdir(target)
self.addCleanup(posix.rmdir, target)
return target

def test_chmod_dir(self):
target = self.tempdir()
self.check_chmod(posix.chmod, target)

@unittest.skipUnless(hasattr(posix, 'lchmod'), 'test needs os.lchmod()')
def test_lchmod_file(self):
self.check_chmod(posix.lchmod, os_helper.TESTFN)
self.check_chmod(posix.chmod, os_helper.TESTFN, follow_symlinks=False)

@unittest.skipUnless(hasattr(posix, 'lchmod'), 'test needs os.lchmod()')
def test_lchmod_dir(self):
target = self.tempdir()
self.check_chmod(posix.lchmod, target)
self.check_chmod(posix.chmod, target, follow_symlinks=False)

def check_chmod_link(self, chmod_func, target, link, **kwargs):
target_mode = os.stat(target).st_mode
link_mode = os.lstat(link).st_mode
try:
new_mode = target_mode & ~(stat.S_IWOTH | stat.S_IWGRP | stat.S_IWUSR)
chmod_func(link, new_mode, **kwargs)
self.assertEqual(os.stat(target).st_mode, new_mode)
self.assertEqual(os.lstat(link).st_mode, link_mode)
new_mode = target_mode | (stat.S_IWOTH | stat.S_IWGRP | stat.S_IWUSR)
chmod_func(link, new_mode, **kwargs)
self.assertEqual(os.stat(target).st_mode, new_mode)
self.assertEqual(os.lstat(link).st_mode, link_mode)
finally:
posix.chmod(target, target_mode)

def check_lchmod_link(self, chmod_func, target, link, **kwargs):
target_mode = os.stat(target).st_mode
link_mode = os.lstat(link).st_mode
new_mode = link_mode & ~(stat.S_IWOTH | stat.S_IWGRP | stat.S_IWUSR)
chmod_func(link, new_mode, **kwargs)
self.assertEqual(os.stat(target).st_mode, target_mode)
self.assertEqual(os.lstat(link).st_mode, new_mode)
new_mode = link_mode | (stat.S_IWOTH | stat.S_IWGRP | stat.S_IWUSR)
chmod_func(link, new_mode, **kwargs)
self.assertEqual(os.stat(target).st_mode, target_mode)
self.assertEqual(os.lstat(link).st_mode, new_mode)

@os_helper.skip_unless_symlink
def test_chmod_file_symlink(self):
target = os_helper.TESTFN
link = os_helper.TESTFN + '-link'
os.symlink(target, link)
self.addCleanup(posix.unlink, link)
if os.name == 'nt':
self.check_lchmod_link(posix.chmod, target, link)
else:
self.check_chmod_link(posix.chmod, target, link)
self.check_chmod_link(posix.chmod, target, link, follow_symlinks=True)

@os_helper.skip_unless_symlink
def test_chmod_dir_symlink(self):
target = self.tempdir()
link = os_helper.TESTFN + '-link'
os.symlink(target, link, target_is_directory=True)
self.addCleanup(posix.unlink, link)
if os.name == 'nt':
self.check_lchmod_link(posix.chmod, target, link)
else:
self.check_chmod_link(posix.chmod, target, link)
self.check_chmod_link(posix.chmod, target, link, follow_symlinks=True)

@unittest.skipUnless(hasattr(posix, 'lchmod'), 'test needs os.lchmod()')
@os_helper.skip_unless_symlink
def test_lchmod_file_symlink(self):
target = os_helper.TESTFN
link = os_helper.TESTFN + '-link'
os.symlink(target, link)
self.addCleanup(posix.unlink, link)
self.check_lchmod_link(posix.chmod, target, link, follow_symlinks=False)
self.check_lchmod_link(posix.lchmod, target, link)

@unittest.skipUnless(hasattr(posix, 'lchmod'), 'test needs os.lchmod()')
@os_helper.skip_unless_symlink
def test_lchmod_dir_symlink(self):
target = self.tempdir()
link = os_helper.TESTFN + '-link'
os.symlink(target, link)
self.addCleanup(posix.unlink, link)
self.check_lchmod_link(posix.chmod, target, link, follow_symlinks=False)
self.check_lchmod_link(posix.lchmod, target, link)

def _test_chflags_regular_file(self, chflags_func, target_file, **kwargs):
st = os.stat(target_file)
self.assertTrue(hasattr(st, 'st_flags'))
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_shutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -1132,10 +1132,11 @@ def test_copymode_symlink_to_symlink(self):
os.lchmod(src_link, stat.S_IRWXO|stat.S_IRWXG)
# link to link
os.lchmod(dst_link, stat.S_IRWXO)
old_mode = os.stat(dst).st_mode
shutil.copymode(src_link, dst_link, follow_symlinks=False)
self.assertEqual(os.lstat(src_link).st_mode,
os.lstat(dst_link).st_mode)
self.assertNotEqual(os.stat(src).st_mode, os.stat(dst).st_mode)
self.assertEqual(os.stat(dst).st_mode, old_mode)
# src link - use chmod
os.lchmod(dst_link, stat.S_IRWXO)
shutil.copymode(src_link, dst, follow_symlinks=False)
Expand Down