diff --git a/tests/ctest/common/CMakeLists.txt b/tests/ctest/common/CMakeLists.txt index dfe66f89035a9..bb5ceb304bbc5 100644 --- a/tests/ctest/common/CMakeLists.txt +++ b/tests/ctest/common/CMakeLists.txt @@ -1,5 +1,6 @@ add_pcsx2_test(common_test byteswap_tests.cpp + filesystem_tests.cpp path_tests.cpp string_util_tests.cpp ) diff --git a/tests/ctest/common/filesystem_tests.cpp b/tests/ctest/common/filesystem_tests.cpp new file mode 100644 index 0000000000000..5df3d629ed2db --- /dev/null +++ b/tests/ctest/common/filesystem_tests.cpp @@ -0,0 +1,42 @@ +// SPDX-FileCopyrightText: 2002-2024 PCSX2 Dev Team +// SPDX-License-Identifier: GPL-3.0+ + +#include "common/FileSystem.h" +#include "common/Path.h" +#include + +#ifdef __linux__ + +#include + +TEST(Filesystem, DontFollowSymbolicLinksWhenRecursivelyDeletingDirectories) +{ + // Find a suitable location to write some test files. + const char* test_dir = "/tmp/pcsx2_filesystem_test"; + FileSystem::CreateDirectoryPath(test_dir, false); + + // Create a target directory containing a file that shouldn't be deleted. + std::string target_dir = Path::Combine(test_dir, "target_dir"); + FileSystem::CreateDirectoryPath(target_dir.c_str(), false); + std::string file_path = Path::Combine(target_dir, "file.txt"); + ASSERT_TRUE(FileSystem::WriteStringToFile(file_path.c_str(), "Lorem ipsum!")); + + // Create a directory containing a symlink to the target directory. + std::string dir_to_delete = Path::Combine(test_dir, "dir_to_delete"); + FileSystem::CreateDirectoryPath(dir_to_delete.c_str(), false); + std::string symlink_path = Path::Combine(dir_to_delete, "link"); + symlink(target_dir.c_str(), symlink_path.c_str()); + + // Delete the directory containing the symlink. + ASSERT_TRUE(FileSystem::RecursiveDeleteDirectory(dir_to_delete.c_str())); + + // Make sure the target file didn't get deleted. + ASSERT_TRUE(FileSystem::FileExists(file_path.c_str())); + + // Clean up. + FileSystem::DeleteFilePath(file_path.c_str()); + FileSystem::DeleteDirectory(target_dir.c_str()); + FileSystem::DeleteDirectory(test_dir); +} + +#endif