Skip to content
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

No PermissionError gets raised on deleting a read-only file #543

Closed
g3n35i5 opened this issue Aug 7, 2020 · 2 comments
Closed

No PermissionError gets raised on deleting a read-only file #543

g3n35i5 opened this issue Aug 7, 2020 · 2 comments

Comments

@g3n35i5
Copy link

g3n35i5 commented Aug 7, 2020

Describe the bug
I'm trying to modify a file in a unittest so that it is in read-only mode and therefore non-deletable unless the user has root privileges.

How To Reproduce
Run the following unittest class:

#!/usr/bin/env python3

import os
import stat

from pyfakefs.fake_filesystem_unittest import TestCase


class TestCaseExample(TestCase):
    def setUp(self):
        self.setUpPyfakefs()

    def test_delete_file_insufficient_permissions(self):
        file_path = "/tmp/file.txt"
        file_content = "Doesn't really matter but hey :)"

        self.fs.create_file(file_path, contents=file_content)
        self.assertTrue(os.path.exists(file_path))

        # Make sure we're not admin
        self.assertFalse(os.getuid() == 0)

        # Make sure that the default mask is -rw-rw-rw
        default_mask = 0o666
        initial_mask = stat.S_IMODE(os.lstat(file_path).st_mode)
        self.assertEqual(default_mask, initial_mask)

        # Make sure that the new mask is -r--r--r--
        read_only_mask = 0o444
        os.chmod(file_path, read_only_mask)
        new_mask = stat.S_IMODE(os.lstat(file_path).st_mode)
        self.assertEqual(read_only_mask, new_mask)

        # Deleting the file should not be possible
        with self.assertRaises(PermissionError):
            os.remove(file_path)

Expected result: The test runs without errors, because the file is in read-only mode and the user is not privileged and therefore must not delete the file.

Actual result: The test fails

Ran 1 test in 0.051s

FAILED (failures=1)

Failure
Traceback (most recent call last):
  File "/usr/lib/python3.8/unittest/case.py", line 60, in testPartExecutor
    yield
  File "/usr/lib/python3.8/unittest/case.py", line 676, in run
    self._callTestMethod(testMethod)
  File "/usr/lib/python3.8/unittest/case.py", line 633, in _callTestMethod
    method()
  File "/tmp/pyfakefs-permissions/main.py", line 36, in test_delete_file_insufficient_permissions
    os.remove(file_path)
  File "/usr/lib/python3.8/unittest/case.py", line 227, in __exit__
    self._raiseFailure("{} not raised".format(exc_name))
  File "/usr/lib/python3.8/unittest/case.py", line 164, in _raiseFailure
    raise self.test_case.failureException(msg)
AssertionError: PermissionError not raised

Your enviroment
Linux-5.7.12-arch1-1-x86_64-with-glibc2.2.5
Python 3.8.5 (default, Jul 27 2020, 08:42:51)
[GCC 10.1.0]
pyfakefs 4.1.0

Notes
Even if I explicitly initialize the environment as non-privileged self.setUpPyfakefs(allow_root_user=False) the problem still exists

Possibly related issues

@mrbean-bremen
Copy link
Member

This is intentionally. Under Unix (not Windows), you can remove such a file, as long as the file directory has write permissions. Here ist the respective test in pyfakefs (in fake_os_test.py):

    def test_remove_file_with_read_permission_shall_succeed_in_posix(self):
        self.check_posix_only()
        path = self.make_path('foo', 'bar')
        self.create_file(path)
        self.os.chmod(path, 0o444)
        self.os.remove(path)
        self.assertFalse(self.os.path.exists(path))

Note that this test (like most pyfakefs tests) runs both in pyfakefs and in the real fs to ensure that the behavior is the same.

@g3n35i5
Copy link
Author

g3n35i5 commented Aug 20, 2020

Thank you for the clarification, I hadn't considered that...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants