Ejemplo n.º 1
0
    def _runner(self, perms_test, perms_after):
        """
        Generic test runner for permissions testing. The umask is set per test
        via the relevant sab config option; the fileystem parameter in setUp().
        Note that the umask set in the environment before starting the program
        also affects the results if sabnzbd.cfg.umask isn't set.

        Arguments:
            str perms_test: permissions for test objects, chmod style "0755".
            str perms_after: expected permissions after completion of the test.
        """
        perms_test = int(perms_test, 8)
        if sabnzbd.cfg.umask():
            perms_after = int(perms_after, 8)
        else:
            perms_after = int("0777", 8) & (sabnzbd.ORG_UMASK ^ int("0777", 8))

        # Setup and verify fake dir
        test_dir = "/test"
        try:
            self.fs.create_dir(test_dir, perms_test)
        except PermissionError:
            ffs.set_uid(0)
            self.fs.create_dir(test_dir, perms_test)
        assert os.path.exists(test_dir) is True
        assert stat.filemode(os.stat(test_dir).st_mode) == "d" + stat.filemode(perms_test)[1:]

        # Setup and verify fake files
        for file in (
            "foobar",
            "file.ext",
            "sub/dir/.nzb",
            "another/sub/dir/WithSome.File",
        ):
            file = os.path.join(test_dir, file)
            try:
                self.fs.create_file(file, perms_test)
            except PermissionError:
                try:
                    ffs.set_uid(0)
                    self.fs.create_file(file, perms_test)
                except Exception:
                    # Skip creating files, if not even using root gets the job done.
                    break
            assert os.path.exists(file) is True
            assert stat.filemode(os.stat(file).st_mode)[1:] == stat.filemode(perms_test)[1:]

        # Set permissions, recursive by default
        filesystem.set_permissions(test_dir)

        # Check the results
        for root, dirs, files in os.walk(test_dir):
            for dir in [os.path.join(root, d) for d in dirs]:
                # Permissions on directories should now match perms_after
                assert stat.filemode(os.stat(dir).st_mode) == "d" + stat.filemode(perms_after)[1:]
            for file in [os.path.join(root, f) for f in files]:
                # Files also shouldn't have any executable or special bits set
                assert (
                    stat.filemode(os.stat(file).st_mode)[1:]
                    == stat.filemode(
                        perms_after & ~(stat.S_ISUID | stat.S_ISGID | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
                    )[1:]
                )

        # Cleanup
        ffs.set_uid(0)
        self.fs.remove_object(test_dir)
        assert os.path.exists(test_dir) is False
        ffs.set_uid(global_uid)
Ejemplo n.º 2
0
"""
tests.test_filesystem - Testing functions in filesystem.py
"""
import stat
import sys

import pyfakefs.fake_filesystem_unittest as ffs

import sabnzbd.cfg
import sabnzbd.filesystem as filesystem
from tests.testhelper import *

# Set the global uid for fake filesystems to a non-root user;
# by default this depends on the user running pytest.
global_uid = 1000
ffs.set_uid(global_uid)


class TestFileFolderNameSanitizer:
    def test_empty(self):
        assert filesystem.sanitize_filename(None) is None
        assert filesystem.sanitize_foldername(None) is None

    @set_platform("win32")
    def test_colon_handling_windows(self):
        assert filesystem.sanitize_filename("test:aftertest") == "test-aftertest"
        assert filesystem.sanitize_filename(":") == "-"
        assert filesystem.sanitize_filename("test:") == "test-"
        assert filesystem.sanitize_filename("test: ") == "test-"
        # They should act the same
        assert filesystem.sanitize_filename("test:aftertest") == filesystem.sanitize_foldername("test:aftertest")