agent: Add is_not_symlink_filter()

Adds a filter that can be used with filter_files() to return only files
that are not symlinks.
This commit is contained in:
Mike Salvatore 2021-06-22 14:59:14 -04:00
parent bfa640444e
commit 14845c659a
3 changed files with 35 additions and 0 deletions

View File

@ -19,3 +19,7 @@ def file_extension_filter(file_extensions: Set):
return f.suffix in file_extensions
return inner_filter
def is_not_symlink_filter(f: Path):
return not f.is_symlink()

View File

@ -1,7 +1,13 @@
import os
import pytest
from tests.utils import is_user_admin
from infection_monkey.utils.dir_utils import (
file_extension_filter,
filter_files,
get_all_regular_files_in_directory,
is_not_symlink_filter,
)
FILES = ["file.jpg.zip", "file.xyz", "1.tar", "2.tgz", "2.png", "2.mpg"]
@ -91,3 +97,19 @@ def test_file_extension_filter(tmp_path):
filtered_files = filter_files(files_in_dir, [file_extension_filter(valid_extensions)])
assert sorted(files[0:2]) == sorted(filtered_files)
@pytest.mark.skipif(
os.name == "nt" and not is_user_admin(), reason="Test requires admin rights on Windows"
)
def test_is_not_symlink_filter(tmp_path):
files = add_files_to_dir(tmp_path)
link_path = tmp_path / "symlink.test"
link_path.symlink_to(files[0], target_is_directory=False)
files_in_dir = get_all_regular_files_in_directory(tmp_path)
filtered_files = filter_files(files_in_dir, [is_not_symlink_filter])
assert link_path in files_in_dir
assert len(filtered_files) == len(FILES)
assert link_path not in filtered_files

9
monkey/tests/utils.py Normal file
View File

@ -0,0 +1,9 @@
import ctypes
import os
def is_user_admin():
if os.name == "posix":
return os.getuid() == 0
return ctypes.windll.shell32.IsUserAnAdmin()