Skip to content

Commit fb7877c

Browse files
Refactor tests to use more of std::filesystem
Because the tests didn't use std::filesystem originally, there was a lot of logic that had to be re-implemented with OS specific function calls. Now that std::filesystem is in use, many things can be refactored: * Replace create_folder with std::filesystem::create_directories * Replace delete_folder & delete_folder_contents with std::filesystem::remove_all * Lazily create folders on first use, resulting in many fewer filesystem operations * FolderManager::copy_file is a wrapper around std::filesystem::copy_file * FolderManager::remove is a wrapper around std::filesystem::remove * Each test uses a separate directory, allowing parallel test execution * FolderManager::add_symlink wraps around std::filesystem::create_symlink, allows for easier usage of symlinks in tests * Use switch in FrameworkEnvironment::add_icd() for consistency * Use error code filesystem calls to get better error messages * Don't make changes to the filesystem when running death tests * Move fs::FolderManager to test_environment since the type was only in test_util so that the shim could get access to it. Now a function pointer is used.
1 parent 345c989 commit fb7877c

12 files changed

+298
-347
lines changed

tests/framework/framework_config.h.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@
7777
#if _WIN32 || _WIN64
7878
#define DUMMY_BINARY_WINDOWS_64 "${CMAKE_CURRENT_SOURCE_DIR}/data/binaries/dummy_library_pe_64.dll"
7979
#define DUMMY_BINARY_WINDOWS_32 "${CMAKE_CURRENT_SOURCE_DIR}/data/binaries/dummy_library_pe_32.dll"
80-
#define BAD_DUMMY_BINARY_WINDOWS_64 "${CMAKE_CURRENT_SOURCE_DIR}/data/binaries/libdummy_library_elf_64.dll"
81-
#define BAD_DUMMY_BINARY_WINDOWS_32 "${CMAKE_CURRENT_SOURCE_DIR}/data/binaries/libdummy_library_elf_32.dll"
80+
#define BAD_DUMMY_BINARY_WINDOWS_64 "${CMAKE_CURRENT_SOURCE_DIR}/data/binaries/dummy_library_elf_64.dll"
81+
#define BAD_DUMMY_BINARY_WINDOWS_32 "${CMAKE_CURRENT_SOURCE_DIR}/data/binaries/dummy_library_elf_32.dll"
8282
#endif
8383
#if defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__GNU__) || defined(__QNX__)
8484
#define DUMMY_BINARY_LINUX_64 "${CMAKE_CURRENT_SOURCE_DIR}/data/binaries/libdummy_library_elf_64.so"

tests/framework/shim/shim.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
#include "test_util.h"
3131

32+
#include <unordered_map>
3233
#include <unordered_set>
3334
#include <stdlib.h>
3435

@@ -135,10 +136,12 @@ struct FrameworkEnvironment; // forward declaration
135136
// defined in the .cpp wont be found by the rest of the application
136137
struct PlatformShim {
137138
PlatformShim() { fputs_stderr_log.reserve(65536); }
138-
PlatformShim(std::vector<fs::FolderManager>* folders) : folders(folders) { fputs_stderr_log.reserve(65536); }
139+
PlatformShim(GetFoldersFunc get_folders_by_name_function) : get_folders_by_name_function(get_folders_by_name_function) {
140+
fputs_stderr_log.reserve(65536);
141+
}
139142

140143
// Used to get info about which drivers & layers have been added to folders
141-
std::vector<fs::FolderManager>* folders;
144+
GetFoldersFunc get_folders_by_name_function;
142145

143146
// Captures the output to stderr from fputs & fputc - aka the output of loader_log()
144147
std::string fputs_stderr_log;
@@ -214,6 +217,8 @@ struct PlatformShim {
214217

215218
std::filesystem::path query_default_redirect_path(ManifestCategory category);
216219

220+
void set_app_package_path(std::filesystem::path const& path);
221+
217222
std::unordered_map<std::string, std::filesystem::path> redirection_map;
218223
std::unordered_map<std::string, std::filesystem::path> dlopen_redirection_map;
219224
std::unordered_set<std::string> known_path_set;
@@ -233,17 +238,14 @@ struct PlatformShim {
233238
std::vector<std::string> parse_env_var_list(std::string const& var);
234239
std::string category_path_name(ManifestCategory category);
235240

236-
std::vector<std::filesystem::path> get_folder_contents(std::vector<fs::FolderManager>* folders,
237-
std::filesystem::path folder_name) noexcept;
238-
239241
extern "C" {
240242
// dynamically link on windows and macos
241243
#if defined(WIN32) || defined(__APPLE__)
242-
using PFN_get_platform_shim = PlatformShim* (*)(std::vector<fs::FolderManager>* folders);
244+
using PFN_get_platform_shim = PlatformShim* (*)(GetFoldersFunc get_folders_by_name_function);
243245
#define GET_PLATFORM_SHIM_STR "get_platform_shim"
244246

245247
#elif defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__GNU__) || defined(__QNX__)
246248
// statically link on linux
247-
PlatformShim* get_platform_shim(std::vector<fs::FolderManager>* folders);
249+
PlatformShim* get_platform_shim(GetFoldersFunc get_folders_by_name_function);
248250
#endif
249251
}

tests/framework/shim/shim_common.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,6 @@ std::vector<std::string> parse_env_var_list(std::string const& var) {
5858
return items;
5959
}
6060

61-
std::vector<std::filesystem::path> get_folder_contents(std::vector<fs::FolderManager>* folders,
62-
std::filesystem::path folder_name) noexcept {
63-
for (auto& folder : *folders) {
64-
if (folder.location() == folder_name) {
65-
return folder.get_files();
66-
}
67-
}
68-
return {};
69-
}
70-
7161
#if defined(WIN32)
7262

7363
D3DKMT_Adapter& D3DKMT_Adapter::add_driver_manifest_path(std::filesystem::path const& src) { return add_path(src, driver_paths); }
@@ -191,6 +181,8 @@ void PlatformShim::add_manifest([[maybe_unused]] ManifestCategory category, [[ma
191181
void PlatformShim::add_unsecured_manifest([[maybe_unused]] ManifestCategory category,
192182
[[maybe_unused]] std::filesystem::path const& path) {}
193183

184+
void PlatformShim::set_app_package_path(std::filesystem::path const& path) {}
185+
194186
void parse_and_add_env_var_override(std::vector<std::string>& paths, std::string env_var_contents) {
195187
auto parsed_paths = parse_env_var_list(env_var_contents);
196188
paths.insert(paths.end(), parsed_paths.begin(), parsed_paths.end());

tests/framework/shim/unix_shim.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@
3636
PlatformShim platform_shim;
3737
extern "C" {
3838
#if defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__GNU__) || defined(__QNX__)
39-
PlatformShim* get_platform_shim(std::vector<fs::FolderManager>* folders) {
40-
platform_shim = PlatformShim(folders);
39+
PlatformShim* get_platform_shim(GetFoldersFunc get_folders_by_name_function) {
40+
platform_shim = PlatformShim(get_folders_by_name_function);
4141
return &platform_shim;
4242
}
4343
#elif defined(__APPLE__)
44-
FRAMEWORK_EXPORT PlatformShim* get_platform_shim(std::vector<fs::FolderManager>* folders) {
45-
platform_shim = PlatformShim(folders);
44+
FRAMEWORK_EXPORT PlatformShim* get_platform_shim(GetFoldersFunc get_folders_by_name_function) {
45+
platform_shim = PlatformShim(get_folders_by_name_function);
4646
return &platform_shim;
4747
}
4848
#endif
@@ -205,7 +205,7 @@ FRAMEWORK_EXPORT struct dirent* READDIR_FUNC_NAME(DIR* dir_stream) {
205205
if (it->is_fake_path) {
206206
real_path = platform_shim.redirection_map.at(it->folder_path);
207207
}
208-
auto filenames = get_folder_contents(platform_shim.folders, real_path);
208+
auto filenames = platform_shim.get_folders_by_name_function(real_path.c_str());
209209

210210
// Add the dirent structures in the order they appear in the FolderManager
211211
// Ignore anything which wasn't in the FolderManager

tests/framework/shim/windows_shim.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -512,8 +512,8 @@ BOOL WINAPI DllMain([[maybe_unused]] HINSTANCE hinst, DWORD dwReason, [[maybe_un
512512
}
513513
return TRUE;
514514
}
515-
FRAMEWORK_EXPORT PlatformShim *get_platform_shim(std::vector<fs::FolderManager> *folders) {
516-
platform_shim = PlatformShim(folders);
515+
FRAMEWORK_EXPORT PlatformShim *get_platform_shim(GetFoldersFunc get_folders_by_name_function) {
516+
platform_shim = PlatformShim(get_folders_by_name_function);
517517
return &platform_shim;
518518
}
519519
}

0 commit comments

Comments
 (0)