aboutsummaryrefslogtreecommitdiff
path: root/libs/usvfs/test/usvfs_test_runner
diff options
context:
space:
mode:
authorSulfurNitride <SulfurNitride@users.noreply.github.com>2026-02-11 02:37:39 -0600
committerSulfurNitride <SulfurNitride@users.noreply.github.com>2026-02-11 02:37:39 -0600
commit7ee008e150bc5bcf76082d726f719ee0fdfda982 (patch)
tree27fb39be241fdb5ac2734c574de678977d1856d0 /libs/usvfs/test/usvfs_test_runner
Fluorine Manager: full Linux port of Mod Organizer 2
Complete native Linux port with FUSE-based virtual filesystem, Proton/umu-run integration, and Flatpak packaging. Key features: - FUSE VFS replacing Windows USVFS (in-process + standalone helper for Flatpak) - Proton/GE-Proton/umu-run launcher with env var forwarding - Flatpak support (sandbox-aware VFS, NXM handler, umu-run) - Wine prefix management UI - Case-insensitive path resolution for Linux filesystems - QSettings-safe INI handling (avoids Bethesda INI corruption) - Portable instance support with auto-generated launcher scripts Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Diffstat (limited to 'libs/usvfs/test/usvfs_test_runner')
-rw-r--r--libs/usvfs/test/usvfs_test_runner/CMakeLists.txt35
-rw-r--r--libs/usvfs/test/usvfs_test_runner/test_file_operations/test_file_operations.cpp496
-rw-r--r--libs/usvfs/test/usvfs_test_runner/test_file_operations/test_filesystem.cpp159
-rw-r--r--libs/usvfs/test/usvfs_test_runner/test_file_operations/test_filesystem.h104
-rw-r--r--libs/usvfs/test/usvfs_test_runner/test_file_operations/test_ntapi.cpp488
-rw-r--r--libs/usvfs/test/usvfs_test_runner/test_file_operations/test_ntapi.h38
-rw-r--r--libs/usvfs/test/usvfs_test_runner/test_file_operations/test_ntdll_declarations.h157
-rw-r--r--libs/usvfs/test/usvfs_test_runner/test_file_operations/test_w32api.cpp370
-rw-r--r--libs/usvfs/test/usvfs_test_runner/test_file_operations/test_w32api.h36
-rw-r--r--libs/usvfs/test/usvfs_test_runner/usvfs_test/usvfs_basic_test.cpp402
-rw-r--r--libs/usvfs/test/usvfs_test_runner/usvfs_test/usvfs_basic_test.h14
-rw-r--r--libs/usvfs/test/usvfs_test_runner/usvfs_test/usvfs_test.cpp242
-rw-r--r--libs/usvfs/test/usvfs_test_runner/usvfs_test/usvfs_test_base.cpp1002
-rw-r--r--libs/usvfs/test/usvfs_test_runner/usvfs_test/usvfs_test_base.h120
-rw-r--r--libs/usvfs/test/usvfs_test_runner/usvfs_test_runner.cpp105
15 files changed, 3768 insertions, 0 deletions
diff --git a/libs/usvfs/test/usvfs_test_runner/CMakeLists.txt b/libs/usvfs/test/usvfs_test_runner/CMakeLists.txt
new file mode 100644
index 0000000..a0a7ca9
--- /dev/null
+++ b/libs/usvfs/test/usvfs_test_runner/CMakeLists.txt
@@ -0,0 +1,35 @@
+cmake_minimum_required(VERSION 3.16)
+
+find_package(GTest CONFIG REQUIRED)
+
+# other executables
+add_executable(test_file_operations
+ test_file_operations/test_file_operations.cpp
+ test_file_operations/test_filesystem.cpp
+ test_file_operations/test_filesystem.h
+ test_file_operations/test_ntapi.cpp
+ test_file_operations/test_ntapi.h
+ test_file_operations/test_ntdll_declarations.h
+ test_file_operations/test_w32api.cpp
+ test_file_operations/test_w32api.h
+)
+usvfs_set_test_properties(test_file_operations FOLDER usvfs_test_runner)
+target_link_libraries(test_file_operations PRIVATE test_utils ntdll)
+
+add_executable(usvfs_test
+ usvfs_test/usvfs_basic_test.cpp
+ usvfs_test/usvfs_basic_test.h
+ usvfs_test/usvfs_test_base.cpp
+ usvfs_test/usvfs_test_base.h
+ usvfs_test/usvfs_test.cpp
+)
+usvfs_set_test_properties(usvfs_test FOLDER usvfs_test_runner)
+usvfs_target_link_usvfs(usvfs_test)
+target_link_libraries(usvfs_test PRIVATE test_utils)
+
+# actual test executable
+add_executable(usvfs_test_runner usvfs_test_runner.cpp)
+usvfs_set_test_properties(usvfs_test_runner FOLDER usvfs_test_runner)
+target_link_libraries(usvfs_test_runner
+ PRIVATE test_utils GTest::gtest GTest::gtest_main)
+add_dependencies(usvfs_test_runner usvfs_test test_file_operations)
diff --git a/libs/usvfs/test/usvfs_test_runner/test_file_operations/test_file_operations.cpp b/libs/usvfs/test/usvfs_test_runner/test_file_operations/test_file_operations.cpp
new file mode 100644
index 0000000..2135df3
--- /dev/null
+++ b/libs/usvfs/test/usvfs_test_runner/test_file_operations/test_file_operations.cpp
@@ -0,0 +1,496 @@
+#include <cstdio>
+#include <stdexcept>
+
+#include <boost/filesystem.hpp>
+#include <boost/type_traits.hpp>
+#include <winapi.h>
+
+#include "test_ntapi.h"
+#include "test_w32api.h"
+#include <test_helpers.h>
+
+#define WIN32_LEAN_AND_MEAN
+#include <Windows.h>
+
+using usvfs::shared::CodePage;
+using usvfs::shared::string_cast;
+
+void print_usage(const char* myname)
+{
+ using namespace std;
+ fprintf(stderr, "usage: %s [<options>] <command> [<command params>] ...\n", myname);
+ fprintf(stderr,
+ "options and commands are parsed and executed in the order they appear.\n");
+ fprintf(stderr, "\nsupported commands:\n");
+ fprintf(
+ stderr,
+ " -list <dir> : lists the given directory and outputs the results.\n");
+ fprintf(stderr, " -listcontents <dir> : lists the given directory, reading all files "
+ "and outputs the results.\n");
+ fprintf(stderr,
+ " -read <file> : reads the given file and outputs the results.\n");
+ fprintf(stderr, " -overwrite <file> <string> : overwrites the file at the given path "
+ "with the given line (creating directories if in recursive mode).\n");
+ fprintf(stderr, " -deleteoverwrite <file> <string> : shorthand for -delete <file> "
+ "-overwrite <file> <string>\n");
+ fprintf(stderr,
+ " -rewrite <file> <string> : rewrites the file at the given path with the "
+ "given line (fails if file doesn't exist; uses read/write access).\n");
+ fprintf(stderr,
+ " -touch <file> : updates last write timestamp of specified file.\n");
+ fprintf(stderr, " -touchw <file> : updates last write timestamp using full "
+ "write permissions.\n");
+ fprintf(stderr, " -delete <file> : deletes the given file.\n");
+ fprintf(stderr, " -copy <src> <dst> : copies the given file.\n");
+ fprintf(stderr, " -copyover <src> <dst> : copies the given file (replacing existing "
+ "destination).\n");
+ fprintf(stderr, " -rename <src> <dst> : renames the given file.\n");
+ fprintf(stderr, " -renameover <src> <dst> : renames the given file (replacing "
+ "existing destination).\n");
+ fprintf(stderr, " -deleterename <src> <dst> : shorthand for -delete <dst> -rename "
+ "<src> <dst>.\n");
+ fprintf(stderr,
+ " -move <src> <dst> : moves the given file (not supported by ntapi).\n");
+ fprintf(stderr, " -moveover <src> <dst> : moves the given file (replacing existing "
+ "destination; not supported by ntapi).\n");
+ fprintf(
+ stderr,
+ " -deletemove <src> <dst> : shorthand for -delete <dst> -move <src> <dst>.\n");
+ fprintf(stderr, " -debug : shows a message box and wait for a debugger "
+ "to connect.\n");
+ fprintf(stderr, "\nsupported options:\n");
+ fprintf(stderr, " -out <file> : file to log output to (use \"-\" for the "
+ "stdout; otherwise path to output should exist).\n");
+ fprintf(stderr, " -out+ <file> : similar to -out but appends the file instead "
+ "of overwriting it.\n");
+ fprintf(stderr, " -cout <file> : similar to -out but does not log PID and "
+ "other info which may change between runs.\n");
+ fprintf(stderr, " -cout+ <file> : similar to -cout but appends the file "
+ "instead of overwriting it.\n");
+ fprintf(stderr, " -r : recursively list/create directories.\n");
+ fprintf(stderr,
+ " -r- : don't recursively list/create directories.\n");
+ fprintf(stderr, " -basedir <dir> : any paths under the basedir will outputed in "
+ "a relative manner (default is current directory).\n");
+ fprintf(stderr,
+ " -w32api : use regular Win32 API for file access (default).\n");
+ fprintf(stderr,
+ " -ntapi : use lower level ntdll functions for file access.\n");
+}
+
+class CommandExecuter
+{
+public:
+ CommandExecuter() : m_output(stdout), m_api(&w32api)
+ {
+ set_basedir(TestFileSystem::current_directory().string().c_str());
+ }
+
+ ~CommandExecuter()
+ {
+ if (m_output && m_output != stdout)
+ fclose(m_output);
+ }
+
+ FILE* output() const { return m_output; }
+
+ bool file_output() const { return m_output != stdout; }
+
+ // Options:
+
+ void set_output(const char* output_file, bool clean, bool append, const char* cmdline)
+ {
+ if (m_output && m_output != stdout) {
+ fprintf(m_output, "#< Output log closed.\n", output_file);
+ fclose(m_output);
+ }
+
+ m_cleanoutput = clean;
+ m_output = nullptr;
+ errno_t err = fopen_s(&m_output, output_file, append ? "at" : "wt");
+ if (err || !m_output)
+ test::throw_testWinFuncFailed("fopen_s", output_file, err);
+ else {
+ if (m_cleanoutput)
+ fprintf(m_output, "#> Output log openned for: %s\n",
+ clean_cmdline_heuristic(cmdline).c_str());
+ else
+ fprintf(m_output, "#> Output log openned for (pid %d): %s\n",
+ GetCurrentProcessId(), cmdline);
+ w32api.set_output(m_output, m_cleanoutput);
+ ntapi.set_output(m_output, m_cleanoutput);
+ }
+ }
+
+ bool cleanoutput() const { return m_cleanoutput; }
+
+ void set_recursive(bool recursive) { m_recursive = recursive; }
+
+ void set_basedir(const char* basedir)
+ {
+ w32api.set_basepath(basedir);
+ ntapi.set_basepath(basedir);
+ }
+
+ void set_ntapi(bool enable)
+ {
+ if (enable)
+ m_api = &ntapi;
+ else
+ m_api = &w32api;
+ }
+
+ // Commands:
+
+ void list(const char* dir, bool read_files)
+ {
+ if (debug_pending())
+ __debugbreak();
+
+ list_impl(m_api->real_path(dir), read_files);
+ }
+
+ void read(const char* path)
+ {
+ if (debug_pending())
+ __debugbreak();
+
+ m_api->read_file(m_api->real_path(path));
+ }
+
+ void overwrite(const char* path, const char* value)
+ {
+ if (debug_pending())
+ __debugbreak();
+
+ const auto& real = real_path_create_if_necessary(path);
+
+ m_api->write_file(real, value, strlen(value), true,
+ TestFileSystem::write_mode::overwrite);
+ }
+
+ void rewrite(const char* path, const char* value)
+ {
+ if (debug_pending())
+ __debugbreak();
+
+ const auto& real = real_path_create_if_necessary(path);
+
+ // Use read/write access when rewriting to "simulate" the harder case where it is
+ // not known if the file is going to actually be changed
+ m_api->write_file(real, value, strlen(value), false,
+ TestFileSystem::write_mode::manual_truncate, true);
+ m_api->write_file(real, "\r\n", 2, false, TestFileSystem::write_mode::append);
+ }
+
+ void touch(const char* path, bool full_write_access)
+ {
+ if (debug_pending())
+ __debugbreak();
+
+ const auto& real = real_path_create_if_necessary(path);
+
+ m_api->touch_file(real, full_write_access);
+ }
+
+ void deletef(const char* path)
+ {
+ if (debug_pending())
+ __debugbreak();
+
+ m_api->delete_file(m_api->real_path(path));
+ }
+
+ void rename(const char* source, const char* destination, bool replace_existing)
+ {
+ if (debug_pending())
+ __debugbreak();
+
+ m_api->copy_file(m_api->real_path(source), m_api->real_path(destination),
+ replace_existing);
+ }
+
+ void rename(const char* source, const char* destination, bool replace_existing,
+ bool allow_copy)
+ {
+ if (debug_pending())
+ __debugbreak();
+
+ m_api->rename_file(m_api->real_path(source), m_api->real_path(destination),
+ replace_existing, allow_copy);
+ }
+
+ void debug() { m_debug_pending = true; }
+
+ bool debug_pending()
+ {
+ if (!m_debug_pending)
+ return false;
+ m_debug_pending = false;
+ if (!IsDebuggerPresent())
+ MessageBoxA(NULL, "Connect a debugger and press OK to trigger a breakpoint",
+ "DEBUG", 0);
+ return IsDebuggerPresent();
+ }
+
+ // Traversal:
+
+ void list_impl(TestFileSystem::path real, bool read_files)
+ {
+ std::vector<TestFileSystem::path> recurse;
+ {
+ auto files = m_api->list_directory(real);
+ fprintf(m_output, ">> Listing directory {%s}:\n",
+ m_api->relative_path(real).string().c_str());
+ for (auto f : files) {
+ if (f.is_dir()) {
+ fprintf(m_output, "[%s] DIR (attributes 0x%x)\n",
+ string_cast<std::string>(f.name, CodePage::UTF8).c_str(),
+ f.attributes);
+ if (m_recursive && f.name != L"." && f.name != L"..")
+ recurse.push_back(real / f.name);
+ } else {
+ fprintf(m_output, "[%s] FILE (attributes 0x%x, %lld bytes)\n",
+ string_cast<std::string>(f.name, CodePage::UTF8).c_str(),
+ f.attributes, f.size);
+ if (read_files)
+ m_api->read_file(real / f.name);
+ }
+ }
+ }
+ for (auto r : recurse)
+ list_impl(r, read_files);
+ }
+
+private:
+ TestFileSystem::path real_path_create_if_necessary(const char* path)
+ {
+ auto real = m_api->real_path(path);
+ if (m_recursive)
+ try {
+ m_api->create_path(real.parent_path());
+ } catch (const std::exception& e) {
+ throw std::runtime_error(
+ std::format("Failed to create_path [{}] : {}",
+ m_api->relative_path(real.parent_path()).string(), e.what()));
+ }
+ return std::move(real);
+ }
+
+ std::string clean_cmdline_arg(const char* arg_start, const char* arg_end)
+ {
+ if (arg_start == arg_end)
+ return std::string();
+ bool quoted = *arg_start == '\"' && *(arg_end - 1) == '\"';
+ const char* last_sep = arg_end;
+ while (last_sep != arg_start && *last_sep != '\\')
+ --last_sep;
+ if (arg_end - arg_start < (quoted ? 5 : 3) || arg_start[0] == '-' ||
+ arg_start[quoted ? 2 : 1] != ':' || last_sep == arg_start)
+ return std::string(arg_start, arg_end);
+ std::string res = quoted ? "\"" : "";
+ res.append(last_sep + 1, arg_end);
+ return res;
+ }
+
+ std::string clean_cmdline_heuristic(const char* cmdline)
+ {
+ std::string res;
+ bool first = true;
+ while (*cmdline) {
+ const char* end = strchr(cmdline, ' ');
+ if (!end)
+ end = cmdline + strlen(cmdline);
+ if (first)
+ first = false;
+ else
+ res.push_back(' ');
+ res += clean_cmdline_arg(cmdline, end);
+ cmdline = end;
+ while (*cmdline == ' ')
+ ++cmdline;
+ }
+ return res;
+ }
+
+ FILE* m_output;
+ bool m_cleanoutput = false;
+ bool m_recursive = false;
+ bool m_debug_pending = false;
+
+ TestFileSystem* m_api;
+ static TestW32Api w32api;
+ static TestNtApi ntapi;
+};
+
+// static
+TestW32Api CommandExecuter::w32api(stdout);
+TestNtApi CommandExecuter::ntapi(stdout);
+
+class abort_invalid_argument : std::exception
+{};
+
+bool verify_args_exist(const char* flag, int params, int index, int count)
+{
+ if (index + params >= count) {
+ fprintf(stderr, "ERROR: %s requires %d arguments\n", flag, params);
+ throw abort_invalid_argument();
+ }
+ return true;
+}
+
+const char* UntouchedCommandLineArguments()
+{
+ const char* cmd = GetCommandLineA();
+ for (; *cmd && *cmd != ' '; ++cmd) {
+ if (*cmd == '"') {
+ int escaped = 0;
+ for (++cmd; *cmd && (*cmd != '"' || escaped % 2 != 0); ++cmd)
+ escaped = *cmd == '\\' ? escaped + 1 : 0;
+ }
+ }
+ while (*cmd == ' ')
+ ++cmd;
+ return cmd;
+}
+
+int main(int argc, char* argv[])
+{
+ bool found_commands = false;
+ CommandExecuter executer;
+
+ TestFileSystem::path exe_path = argv[0];
+ std::string exename = exe_path.filename().string();
+ std::string cmdline = exename + " " + UntouchedCommandLineArguments();
+ fprintf(stdout, "#> process %d started with commandline: %s\n", GetCurrentProcessId(),
+ cmdline.c_str());
+
+ for (int ai = 1; ai < argc; ++ai) {
+ try {
+ SetLastError(0);
+
+ // options:
+ if (strcmp(argv[ai], "-out") == 0 && verify_args_exist("-out", 1, ai, argc) ||
+ strcmp(argv[ai], "-out+") == 0 && verify_args_exist("-out+", 1, ai, argc) ||
+ strcmp(argv[ai], "-cout") == 0 && verify_args_exist("-cout", 1, ai, argc) ||
+ strcmp(argv[ai], "-cout+") == 0 && verify_args_exist("-cout+", 1, ai, argc)) {
+ bool clean = argv[ai][1] == 'c';
+ bool append = argv[ai][clean ? 5 : 4] == '+';
+ executer.set_output(argv[++ai], clean, append, cmdline.c_str());
+ } else if (strcmp(argv[ai], "-r") == 0)
+ executer.set_recursive(true);
+ else if (strcmp(argv[ai], "-r-") == 0)
+ executer.set_recursive(false);
+ else if (strcmp(argv[ai], "-basedir") == 0 &&
+ verify_args_exist("-basedir", 1, ai, argc)) {
+ executer.set_basedir(argv[++ai]);
+ } else if (strcmp(argv[ai], "-w32api") == 0)
+ executer.set_ntapi(false);
+ else if (strcmp(argv[ai], "-ntapi") == 0)
+ executer.set_ntapi(true);
+ // commands:
+ else if ((strcmp(argv[ai], "-list") == 0 ||
+ strcmp(argv[ai], "-listcontents") == 0) &&
+ verify_args_exist("-list", 1, ai, argc)) {
+ bool contents = strcmp(argv[ai], "-listcontents") == 0;
+ executer.list(argv[++ai], contents);
+ found_commands = true;
+ } else if (strcmp(argv[ai], "-read") == 0 &&
+ verify_args_exist("-read", 1, ai, argc)) {
+ executer.read(argv[++ai]);
+ found_commands = true;
+ } else if (strcmp(argv[ai], "-overwrite") == 0 &&
+ verify_args_exist("-overwrite", 2, ai, argc) ||
+ strcmp(argv[ai], "-deleteoverwrite") == 0 &&
+ verify_args_exist("-deleteoverwrite", 2, ai, argc)) {
+ if (argv[ai][1] == 'd')
+ executer.deletef(argv[ai + 1]);
+ executer.overwrite(argv[ai + 1], argv[ai + 2]);
+ ++ ++ai;
+ found_commands = true;
+ } else if (strcmp(argv[ai], "-rewrite") == 0 &&
+ verify_args_exist("-rewrite", 2, ai, argc)) {
+ executer.rewrite(argv[ai + 1], argv[ai + 2]);
+ ++ ++ai;
+ found_commands = true;
+ } else if (strcmp(argv[ai], "-touch") == 0 &&
+ verify_args_exist("-touch", 1, ai, argc) ||
+ strcmp(argv[ai], "-touchw") == 0 &&
+ verify_args_exist("-touchw", 1, ai, argc)) {
+ bool write_access = argv[ai][6] == 'w';
+ executer.touch(argv[++ai], write_access);
+ found_commands = true;
+ } else if (strcmp(argv[ai], "-delete") == 0 &&
+ verify_args_exist("-delete", 1, ai, argc)) {
+ executer.deletef(argv[++ai]);
+ found_commands = true;
+ } else if (strcmp(argv[ai], "-copy") == 0 &&
+ verify_args_exist("-copy", 2, ai, argc) ||
+ strcmp(argv[ai], "-copyover") == 0 &&
+ verify_args_exist("-copyover", 2, ai, argc)) {
+ bool over = argv[ai][5] == 'o';
+ executer.rename(argv[ai + 1], argv[ai + 2], over);
+ ++ ++ai;
+ found_commands = true;
+ } else if (strcmp(argv[ai], "-rename") == 0 &&
+ verify_args_exist("-rename", 2, ai, argc) ||
+ strcmp(argv[ai], "-renameover") == 0 &&
+ verify_args_exist("-renameover", 2, ai, argc) ||
+ strcmp(argv[ai], "-deleterename") == 0 &&
+ verify_args_exist("-deleterename", 2, ai, argc) ||
+ strcmp(argv[ai], "-move") == 0 &&
+ verify_args_exist("-move", 2, ai, argc) ||
+ strcmp(argv[ai], "-moveover") == 0 &&
+ verify_args_exist("-moveover", 2, ai, argc) ||
+ strcmp(argv[ai], "-deletemove") == 0 &&
+ verify_args_exist("-deletemove", 2, ai, argc)) {
+ bool del = argv[ai][1] == 'd';
+ bool move = argv[ai][del ? 7 : 1] == 'm';
+ bool over = !del && argv[ai][move ? 5 : 7] == 'o';
+ if (del)
+ executer.deletef(argv[ai + 2]);
+ executer.rename(argv[ai + 1], argv[ai + 2], over, move);
+ ++ ++ai;
+ found_commands = true;
+ } else if (strcmp(argv[ai], "-debug") == 0) {
+ executer.debug();
+ } else {
+ if (executer.file_output())
+ fprintf(executer.output(), "ERROR: invalid argument {%s}\n", argv[ai]);
+ fprintf(stderr, "ERROR: invalid argument {%s}\n", argv[ai]);
+ return 1;
+ }
+ } catch (const abort_invalid_argument&) {
+ return 1;
+ }
+#if 1 // just a convient way to not catch exception when debugging
+ catch (const std::exception& e) {
+ if (executer.file_output())
+ fprintf(executer.output(), "ERROR: %hs\n", e.what());
+ fprintf(stderr, "ERROR: %hs\n", e.what());
+ return 1;
+ } catch (...) {
+ if (executer.file_output())
+ fprintf(executer.output(), "ERROR: unknown exception");
+ fprintf(stderr, "ERROR: unknown exception\n");
+ return 1;
+ }
+#endif
+ }
+
+ if (!found_commands) {
+ print_usage(exename.c_str());
+ return 2;
+ }
+
+ if (executer.file_output())
+ if (executer.cleanoutput())
+ fprintf(executer.output(), "#< %s ended properly.\n", exename.c_str());
+ else
+ fprintf(executer.output(), "#< %s ended properly in process %d.\n",
+ exename.c_str(), GetCurrentProcessId());
+ fprintf(stdout, "#< %s ended properly in process %d.\n", exename.c_str(),
+ GetCurrentProcessId());
+
+ return 0;
+}
diff --git a/libs/usvfs/test/usvfs_test_runner/test_file_operations/test_filesystem.cpp b/libs/usvfs/test/usvfs_test_runner/test_file_operations/test_filesystem.cpp
new file mode 100644
index 0000000..dcfd5a1
--- /dev/null
+++ b/libs/usvfs/test/usvfs_test_runner/test_file_operations/test_filesystem.cpp
@@ -0,0 +1,159 @@
+
+#include "test_filesystem.h"
+#include <test_helpers.h>
+#define WIN32_LEAN_AND_MEAN
+#include <Windows.h>
+
+using test::throw_testWinFuncFailed;
+
+bool TestFileSystem::FileInformation::is_dir() const
+{
+ return (attributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
+}
+
+bool TestFileSystem::FileInformation::is_file() const
+{
+ return (attributes & FILE_ATTRIBUTE_DIRECTORY) == 0;
+}
+
+TestFileSystem::TestFileSystem(FILE* output) : m_output(output) {}
+
+TestFileSystem::path TestFileSystem::current_directory()
+{
+ DWORD res = GetCurrentDirectoryW(0, NULL);
+ if (!res)
+ throw_testWinFuncFailed("GetCurrentDirectory", res);
+ std::wstring buf(res + 1, '\0');
+ res = GetCurrentDirectoryW(buf.length(), &buf[0]);
+ if (!res || res >= buf.length())
+ throw_testWinFuncFailed("GetCurrentDirectory", res);
+ buf.resize(res);
+ return buf;
+}
+
+TestFileSystem::path TestFileSystem::relative_path(path full_path)
+{
+ return test::path_as_relative(m_basepath, full_path);
+}
+
+// static
+const char* TestFileSystem::write_operation_name(write_mode mode)
+{
+ switch (mode) {
+ case write_mode::manual_truncate:
+ return "Writing file (by open & truncate)";
+ case write_mode::truncate:
+ return "Truncating file";
+ case write_mode::create:
+ return "Creating file";
+ case write_mode::overwrite:
+ return "Overwriting file";
+ case write_mode::opencreate:
+ return "Opening/Creating file";
+ case write_mode::append:
+ return "Appending file";
+ }
+ return "Unknown write operation?!";
+}
+
+// static
+const char* TestFileSystem::rename_operation_name(bool replace_existing,
+ bool allow_copy)
+{
+ if (allow_copy)
+ return replace_existing ? "Moving file over" : "Moving file";
+ else
+ return replace_existing ? "Renaming file over" : "Renaming file";
+}
+
+void TestFileSystem::print_operation(const char* operation, const path& target)
+{
+ if (m_output)
+ fprintf(m_output, "# (%s) %s {%s}\n", id(), operation,
+ relative_path(target).string().c_str());
+}
+
+void TestFileSystem::print_operation(const char* operation, const path& source,
+ const path& target)
+{
+ if (m_output)
+ fprintf(m_output, "# (%s) %s {%s} {%s}\n", id(), operation,
+ relative_path(source).string().c_str(),
+ relative_path(target).string().c_str());
+}
+
+static inline void print_op_with_result(FILE* output, const char* prefix,
+ const char* operation, const uint32_t* result,
+ DWORD* last_error, const char* opt_arg)
+{
+ if (output) {
+ fprintf(output, "%s%s", prefix, operation);
+ if (opt_arg)
+ fprintf(output, " %s", opt_arg);
+ if (result)
+ fprintf(output, " returned %u (0x%x)", *result, *result);
+ if (last_error)
+ fprintf(output, " last error %u (0x%x)", *last_error, *last_error);
+ fprintf(output, "\n");
+ }
+}
+
+void TestFileSystem::print_result(const char* operation, uint32_t result,
+ bool with_last_error, const char* opt_arg,
+ bool hide_result)
+{
+ if (m_output) {
+ DWORD last_error = GetLastError();
+ std::string prefix = "# (";
+ prefix += id();
+ prefix += ") ";
+ print_op_with_result(m_output, prefix.c_str(), operation,
+ hide_result ? nullptr : &result,
+ with_last_error ? &last_error : nullptr, opt_arg);
+ SetLastError(last_error);
+ }
+}
+
+void TestFileSystem::print_error(const char* operation, uint32_t result,
+ bool with_last_error, const char* opt_arg)
+{
+ DWORD last_error = with_last_error ? GetLastError() : 0;
+ print_op_with_result(stderr, "ERROR: ", operation, &result,
+ with_last_error ? &last_error : nullptr, opt_arg);
+ if (m_output && m_output != stdout)
+ print_op_with_result(m_output, "ERROR: ", operation, &result,
+ with_last_error ? &last_error : nullptr, opt_arg);
+}
+
+void TestFileSystem::print_write_success(const void* data, std::size_t size,
+ std::size_t written)
+{
+ if (m_output) {
+ fprintf(m_output, "# Successfully written %u bytes ",
+ static_cast<unsigned>(written));
+ // heuristics to print nicer one liners:
+ if (size == 1 && reinterpret_cast<const char*>(data)[0] == '\n' ||
+ size == 2 && reinterpret_cast<const char*>(data)[0] == '\r' &&
+ reinterpret_cast<const char*>(data)[1] == '\n')
+ fprintf(m_output, "<newline>");
+ else {
+ fprintf(m_output, "{");
+ if (size && reinterpret_cast<const char*>(data)[size - 1] == '\n')
+ --size;
+ if (data)
+ fwrite(data, 1, size, m_output);
+ else if (size)
+ fwrite("NULL?!", 1, 6, m_output);
+ fprintf(m_output, "}");
+ }
+ fprintf(output(), "\n");
+ }
+}
+
+uint32_t TestFileSystem::clean_attributes(uint32_t attr)
+{
+ if (m_cleanoutput)
+ return attr & ~(FILE_ATTRIBUTE_NOT_CONTENT_INDEXED);
+ else
+ return attr;
+}
diff --git a/libs/usvfs/test/usvfs_test_runner/test_file_operations/test_filesystem.h b/libs/usvfs/test/usvfs_test_runner/test_file_operations/test_filesystem.h
new file mode 100644
index 0000000..3a5f53b
--- /dev/null
+++ b/libs/usvfs/test/usvfs_test_runner/test_file_operations/test_filesystem.h
@@ -0,0 +1,104 @@
+#pragma once
+
+#include <cstdio>
+#include <filesystem>
+#include <string>
+#include <vector>
+
+class TestFileSystem
+{
+public:
+ static constexpr auto FILE_CONTENTS_PRINT_PREFIX = "== ";
+
+ typedef std::filesystem::path path;
+ typedef std::FILE FILE;
+
+ static path current_directory();
+
+ TestFileSystem(FILE* output);
+
+ void set_output(FILE* output, bool clean)
+ {
+ m_output = output;
+ m_cleanoutput = clean;
+ }
+
+ // base path used to trim outputs (which is important so we can compare tests ran at
+ // different base paths)
+ void set_basepath(const char* path) { m_basepath = real_path(path); }
+
+ // returns the path relative to the base path
+ path relative_path(path full_path);
+
+ virtual const char* id() = 0;
+
+ virtual path real_path(const char* abs_or_rel_path) = 0;
+
+ struct FileInformation
+ {
+ std::wstring name;
+ uint32_t attributes;
+ uint64_t size;
+
+ FileInformation(const std::wstring& iname, uint32_t iattributes, uint64_t isize)
+ : name(iname), attributes(iattributes), size(isize)
+ {}
+
+ bool is_dir() const;
+ bool is_file() const;
+ };
+ typedef std::vector<FileInformation> FileInfoList;
+
+ virtual FileInfoList list_directory(const path& directory_path) = 0;
+
+ virtual void create_path(const path& directory_path) = 0;
+
+ virtual void read_file(const path& file_path) = 0;
+
+ enum class write_mode
+ {
+ manual_truncate,
+ truncate,
+ create,
+ overwrite,
+ opencreate,
+ append
+ };
+ virtual void write_file(const path& file_path, const void* data, std::size_t size,
+ bool add_new_line, write_mode mode,
+ bool rw_access = false) = 0;
+
+ virtual void touch_file(const path& file_path, bool full_write_access = false) = 0;
+
+ virtual void delete_file(const path& file_path) = 0;
+
+ virtual void copy_file(const path& source_path, const path& destination_path,
+ bool replace_existing) = 0;
+
+ virtual void rename_file(const path& source_path, const path& destination_path,
+ bool replace_existing, bool allow_copy) = 0;
+
+protected:
+ FILE* output() { return m_output; }
+ bool cleanoutput() const { return m_cleanoutput; }
+ static const char* write_operation_name(write_mode mode);
+ static const char* rename_operation_name(bool replace_existing, bool allow_copy);
+
+public: // mainly for derived class (but also used by helper classes like SafeHandle so
+ // public)
+ void print_operation(const char* operation, const path& target);
+ void print_operation(const char* operation, const path& source, const path& target);
+ void print_result(const char* operation, uint32_t result,
+ bool with_last_error = false, const char* opt_arg = nullptr,
+ bool hide_result = false);
+ void print_error(const char* operation, uint32_t result, bool with_last_error = false,
+ const char* opt_arg = nullptr);
+ void print_write_success(const void* data, std::size_t size, std::size_t written);
+
+ uint32_t clean_attributes(uint32_t attr);
+
+private:
+ FILE* m_output;
+ bool m_cleanoutput = false;
+ path m_basepath;
+};
diff --git a/libs/usvfs/test/usvfs_test_runner/test_file_operations/test_ntapi.cpp b/libs/usvfs/test/usvfs_test_runner/test_file_operations/test_ntapi.cpp
new file mode 100644
index 0000000..34966b5
--- /dev/null
+++ b/libs/usvfs/test/usvfs_test_runner/test_file_operations/test_ntapi.cpp
@@ -0,0 +1,488 @@
+
+#include "test_ntapi.h"
+#include <cstdio>
+#include <cstring>
+#include <test_helpers.h>
+#include <vector>
+
+#define WIN32_LEAN_AND_MEAN
+#include <Windows.h>
+#include <Winternl.h>
+#include <stdio.h>
+
+#include "test_ntdll_declarations.h"
+
+class TestNtApi::SafeHandle
+{
+public:
+ SafeHandle(TestFileSystem* tfs, HANDLE handle = NULL) : m_handle(handle), m_tfs(tfs)
+ {}
+ SafeHandle(const SafeHandle&) = delete;
+ SafeHandle(SafeHandle&& other) : m_handle(other.m_handle), m_tfs(other.m_tfs)
+ {
+ other.m_handle = nullptr;
+ }
+
+ operator HANDLE() { return m_handle; }
+ operator PHANDLE() { return &m_handle; }
+
+ bool valid() const { return m_handle != NULL; }
+
+ ~SafeHandle()
+ {
+ if (m_handle) {
+ NTSTATUS status = NtClose(m_handle);
+ if (m_tfs)
+ m_tfs->print_result("NtClose", status);
+ if (!NT_SUCCESS(status))
+ if (m_tfs)
+ m_tfs->print_error("NtClose", status);
+ else
+ std::fprintf(stderr, "NtClose failed : 0x%lx", status);
+ m_handle = NULL;
+ }
+ }
+
+private:
+ HANDLE m_handle;
+ TestFileSystem* m_tfs;
+};
+
+const char* TestNtApi::id()
+{
+ return "Nt";
+}
+
+TestNtApi::path TestNtApi::real_path(const char* abs_or_rel_path)
+{
+ if (!abs_or_rel_path || !abs_or_rel_path[0])
+ return path();
+
+ static constexpr char nt_path_prefix[] = "\\??\\";
+ static constexpr wchar_t nt_path_prefix_w[] = L"\\??\\";
+
+ bool path_dos = strncmp(abs_or_rel_path, nt_path_prefix, strlen(nt_path_prefix)) == 0;
+ bool path_has_drive = abs_or_rel_path[1] == ':';
+ bool path_unc = abs_or_rel_path[0] == '\\' && abs_or_rel_path[1] == '\\';
+ bool path_absolute = path_has_drive || abs_or_rel_path[0] == '\\';
+
+ path result;
+ if (!path_dos) {
+ if (!path_unc)
+ result.assign(nt_path_prefix_w);
+ if (!path_absolute)
+ result /= current_directory();
+ else if (!path_has_drive && !path_unc)
+ // if "absolute" path but without a drive letter (i.e. \windows)
+ // the take the drive from the current directory: (i.e. "C:")
+ result /= current_directory().root_name();
+ }
+
+ int result_size = 0;
+ for (auto r : result)
+ ++result_size;
+
+ // now append abs_or_rel_path, handling ".." and "." properly:
+ path arp{abs_or_rel_path};
+ int base_size = path_unc ? 3 : 4;
+ for (auto p : arp) {
+ if (p == "..") {
+ if (result_size > base_size) { // refuse to remove top level element (i.e.
+ // \??\C:\ which is 4 elements)
+ result.remove_filename();
+ --result_size;
+ }
+ } else if (!p.empty() && p != ".") {
+ result /= p;
+ ++result_size;
+ }
+ }
+
+ return result;
+}
+
+TestNtApi::SafeHandle TestNtApi::open_directory(const path& directory_path, bool create,
+ bool allow_non_existence, long* pstatus)
+{
+ print_operation(create ? "Creating directory" : "Openning directory", directory_path);
+
+ UNICODE_STRING unicode_path;
+ RtlInitUnicodeString(&unicode_path, directory_path.c_str());
+
+ OBJECT_ATTRIBUTES attributes;
+ InitializeObjectAttributes(&attributes, &unicode_path, OBJ_CASE_INSENSITIVE, NULL,
+ NULL);
+
+ SafeHandle dir(this);
+ IO_STATUS_BLOCK iosb;
+ NTSTATUS status = NtCreateFile(
+ dir, FILE_LIST_DIRECTORY | FILE_TRAVERSE | SYNCHRONIZE, &attributes, &iosb, NULL,
+ FILE_ATTRIBUTE_DIRECTORY, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
+ create ? FILE_OPEN_IF : FILE_OPEN,
+ FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0);
+
+ print_result("NtCreateFile", status);
+
+ if (pstatus)
+ *pstatus = status;
+ if ((status == STATUS_OBJECT_NAME_NOT_FOUND ||
+ status == STATUS_OBJECT_PATH_NOT_FOUND) &&
+ allow_non_existence)
+ return NULL;
+ if (!NT_SUCCESS(status))
+ throw test::FuncFailed("NtCreateFile", status);
+ if (!NT_SUCCESS(iosb.Status))
+ throw test::FuncFailed("NtCreateFile", "bad iosb.Status", iosb.Status);
+
+ return dir;
+}
+
+TestFileSystem::FileInfoList TestNtApi::list_directory(const path& directory_path)
+{
+ SafeHandle dir = open_directory(directory_path, false);
+
+ print_operation("Querying directory", directory_path);
+
+ FileInfoList files;
+ while (true) {
+ char buf[4096]{0};
+ IO_STATUS_BLOCK iosb;
+
+ NTSTATUS status =
+ NtQueryDirectoryFile(dir, NULL, NULL, NULL, &iosb, buf, sizeof(buf),
+ MyFileBothDirectoryInformation, FALSE, NULL, FALSE);
+ print_result("NtQueryDirectoryFile", status);
+
+ if (status == STATUS_NO_MORE_FILES)
+ break;
+ if (!NT_SUCCESS(status))
+ throw test::FuncFailed("NtQueryDirectoryFile", status);
+ if (!NT_SUCCESS(iosb.Status))
+ throw test::FuncFailed("NtQueryDirectoryFile", "bad iosb.Status", iosb.Status);
+ if (iosb.Information == 0) // This shouldn't happend unless the filename (not full
+ // path) is larger then sizeof(buf)
+ throw test::FuncFailed("NtQueryDirectoryFile", "buffer too small",
+ iosb.Information);
+
+ PFILE_BOTH_DIR_INFORMATION info = reinterpret_cast<PFILE_BOTH_DIR_INFORMATION>(buf);
+ while (true) {
+ files.push_back(FileInformation(
+ std::wstring(info->FileName,
+ info->FileNameLength / sizeof(info->FileName[0])),
+ clean_attributes(info->FileAttributes), info->EndOfFile.QuadPart));
+ if (info->NextEntryOffset)
+ info = reinterpret_cast<PFILE_BOTH_DIR_INFORMATION>(
+ reinterpret_cast<char*>(info) + info->NextEntryOffset);
+ else
+ break;
+ }
+ }
+
+ return files;
+}
+
+void TestNtApi::create_path(const path& directory_path)
+{
+ // sanity and guaranteed recursion end:
+ if (!directory_path.has_relative_path())
+ throw std::runtime_error("Refusing to create non-existing top level path");
+
+ // if directory already exists all is good
+ NTSTATUS status;
+ if (open_directory(directory_path, false, true, &status).valid())
+ return;
+
+ if (status != STATUS_OBJECT_NAME_NOT_FOUND) // STATUS_OBJECT_NAME_NOT_FOUND means
+ // parent directory already exists
+ create_path(directory_path
+ .parent_path()); // otherwise create parent directory (recursively)
+
+ open_directory(directory_path, true);
+}
+
+void TestNtApi::read_file(const path& file_path)
+{
+ print_operation("Reading file", file_path);
+
+ UNICODE_STRING unicode_path;
+ RtlInitUnicodeString(&unicode_path, file_path.c_str());
+
+ OBJECT_ATTRIBUTES attributes;
+ InitializeObjectAttributes(&attributes, &unicode_path, OBJ_CASE_INSENSITIVE, NULL,
+ NULL);
+
+ SafeHandle file(this);
+ IO_STATUS_BLOCK iosb;
+ NTSTATUS status = NtOpenFile(file, GENERIC_READ | SYNCHRONIZE, &attributes, &iosb,
+ FILE_SHARE_READ, FILE_SYNCHRONOUS_IO_NONALERT);
+ print_result("NtOpenFile", status);
+
+ if (!NT_SUCCESS(status))
+ throw test::FuncFailed("NtOpenFile", status);
+ if (!NT_SUCCESS(iosb.Status))
+ throw test::FuncFailed("NtOpenFile", "bad iosb.Status", iosb.Status);
+
+ uint32_t total = 0;
+ bool ends_with_newline = true;
+ bool pending_prefix = true;
+ while (true) {
+ char buf[4096];
+
+ memset(&iosb, 0, sizeof(iosb));
+ status = NtReadFile(file, NULL, NULL, NULL, &iosb, buf, sizeof(buf), NULL, NULL);
+ print_result("NtReadFile", status);
+ if (status == STATUS_END_OF_FILE)
+ break;
+ if (!NT_SUCCESS(status))
+ throw test::FuncFailed("NtReadFile", status);
+
+ total += iosb.Information;
+ char* begin = buf;
+ char* end = begin + iosb.Information;
+ while (begin != end) {
+ if (pending_prefix) {
+ if (output())
+ fwrite(FILE_CONTENTS_PRINT_PREFIX, 1, strlen(FILE_CONTENTS_PRINT_PREFIX),
+ output());
+ pending_prefix = false;
+ }
+ bool skip_newline = false;
+ char* print_end = reinterpret_cast<char*>(std::memchr(begin, '\n', end - begin));
+ if (print_end) {
+ pending_prefix = true;
+ if (print_end > begin && *(print_end - 1) == '\r') {
+ // convert \r\n => \n:
+ *(print_end - 1) = '\n';
+ skip_newline = true;
+ } else // only a '\n' so just print it
+ ++print_end;
+ } else {
+ print_end = end;
+ if (print_end > begin && *(print_end - 1) == '\r') {
+ // buffer ends with \r so skip it under the hope it will be followed with a \n
+ --print_end;
+ skip_newline = true;
+ }
+ }
+ if (output())
+ fwrite(begin, 1, print_end - begin, output());
+ ends_with_newline = print_end > begin && *(print_end - 1) == '\n';
+ begin = print_end;
+ if (skip_newline)
+ ++begin;
+ }
+ if (output() && !ends_with_newline) {
+ fwrite("\n", 1, 1, output());
+ ends_with_newline = true;
+ }
+ }
+ if (output()) {
+ fprintf(output(), "# Successfully read %u bytes.\n", total);
+ }
+}
+
+void TestNtApi::write_file(const path& file_path, const void* data, std::size_t size,
+ bool add_new_line, write_mode mode, bool rw_access)
+{
+ print_operation(write_operation_name(mode), file_path);
+
+ UNICODE_STRING unicode_path;
+ RtlInitUnicodeString(&unicode_path, file_path.c_str());
+
+ OBJECT_ATTRIBUTES attributes;
+ InitializeObjectAttributes(&attributes, &unicode_path, OBJ_CASE_INSENSITIVE, NULL,
+ NULL);
+
+ ACCESS_MASK access = GENERIC_WRITE | SYNCHRONIZE;
+ ULONG disposition = FILE_OPEN;
+ switch (mode) {
+ case write_mode::truncate:
+ disposition = FILE_OVERWRITE;
+ break;
+ case write_mode::create:
+ disposition = FILE_CREATE;
+ break;
+ case write_mode::overwrite:
+ disposition = FILE_SUPERSEDE;
+ break;
+ case write_mode::opencreate:
+ disposition = FILE_OPEN_IF;
+ break;
+ case write_mode::append:
+ disposition = FILE_OPEN_IF;
+ access = FILE_APPEND_DATA | SYNCHRONIZE;
+ break;
+ }
+ if (rw_access)
+ access |= GENERIC_READ;
+
+ SafeHandle file(this);
+ IO_STATUS_BLOCK iosb;
+ NTSTATUS status =
+ NtCreateFile(file, access, &attributes, &iosb, NULL, FILE_ATTRIBUTE_NORMAL, 0,
+ disposition, FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0);
+ print_result("NtCreateFile", status);
+
+ if (!NT_SUCCESS(status))
+ throw test::FuncFailed("NtCreateFile", status);
+ if (!NT_SUCCESS(iosb.Status))
+ throw test::FuncFailed("NtCreateFile", "bad iosb.Status", iosb.Status);
+
+ if (mode == write_mode::manual_truncate) {
+ FILE_END_OF_FILE_INFORMATION eofinfo{0};
+ status = NtSetInformationFile(file, &iosb, &eofinfo, sizeof(eofinfo),
+ MyFileEndOfFileInformation);
+ print_result("NtSetInformationFile", status, false, "EOF");
+
+ if (!NT_SUCCESS(status))
+ throw test::FuncFailed("NtSetInformationFile", status);
+ if (!NT_SUCCESS(iosb.Status))
+ throw test::FuncFailed("NtSetInformationFile", "bad iosb.Status", iosb.Status);
+ }
+
+ // finally write the data:
+ size_t total = 0;
+
+ if (data) {
+ status = NtWriteFile(file, NULL, NULL, NULL, &iosb, const_cast<void*>(data),
+ static_cast<ULONG>(size), NULL, NULL);
+ print_result("NtWriteFile", status);
+ if (!NT_SUCCESS(status))
+ throw test::FuncFailed("NtWriteFile", status);
+ if (!NT_SUCCESS(iosb.Status))
+ throw test::FuncFailed("NtWriteFile", "bad iosb.Status", iosb.Status);
+ total += iosb.Information;
+
+ if (add_new_line) {
+ char buffer[] = "\r\n";
+ status = NtWriteFile(file, NULL, NULL, NULL, &iosb, buffer, 2, NULL, NULL);
+ print_result("NtWriteFile", status);
+ if (!NT_SUCCESS(status))
+ throw test::FuncFailed("NtWriteFile", status);
+ if (!NT_SUCCESS(iosb.Status))
+ throw test::FuncFailed("NtWriteFile", "bad iosb.Status", iosb.Status);
+ total += iosb.Information;
+ }
+ }
+
+ print_write_success(data, size, total);
+}
+
+void TestNtApi::touch_file(const path& file_path, bool full_write_access)
+{
+ print_operation("Touching file", file_path);
+
+ SYSTEMTIME st;
+ GetSystemTime(&st);
+ FILETIME ft;
+ if (!SystemTimeToFileTime(&st, &ft))
+ test::throw_testWinFuncFailed("SystemTimeToFileTime");
+
+ UNICODE_STRING unicode_path;
+ RtlInitUnicodeString(&unicode_path, file_path.c_str());
+
+ OBJECT_ATTRIBUTES attributes;
+ InitializeObjectAttributes(&attributes, &unicode_path, OBJ_CASE_INSENSITIVE, NULL,
+ NULL);
+
+ SafeHandle file(this);
+ IO_STATUS_BLOCK iosb;
+ auto share_all = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
+ auto access =
+ (full_write_access ? GENERIC_WRITE : FILE_WRITE_ATTRIBUTES) | SYNCHRONIZE;
+ NTSTATUS status =
+ NtCreateFile(file, access, &attributes, &iosb, NULL, FILE_ATTRIBUTE_NORMAL,
+ share_all, FILE_OPEN_IF, FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0);
+ print_result("NtCreateFile", status);
+
+ if (!NT_SUCCESS(status))
+ throw test::FuncFailed("NtCreateFile", status);
+ if (!NT_SUCCESS(iosb.Status))
+ throw test::FuncFailed("NtCreateFile", "bad iosb.Status", iosb.Status);
+
+ FILE_BASIC_INFORMATION basicinfo{0};
+ basicinfo.LastWriteTime.LowPart = ft.dwLowDateTime;
+ basicinfo.LastWriteTime.HighPart = ft.dwHighDateTime;
+ status = NtSetInformationFile(file, &iosb, &basicinfo, sizeof(basicinfo),
+ MyFileBasicInformation);
+ print_result("NtSetInformationFile", status, false, "Basic");
+
+ if (!NT_SUCCESS(status))
+ throw test::FuncFailed("NtSetInformationFile", status);
+ if (!NT_SUCCESS(iosb.Status))
+ throw test::FuncFailed("NtSetInformationFile", "bad iosb.Status", iosb.Status);
+}
+
+void TestNtApi::delete_file(const path& file_path)
+{
+ print_operation("Deleting file", file_path);
+
+ UNICODE_STRING unicode_path;
+ RtlInitUnicodeString(&unicode_path, file_path.c_str());
+
+ OBJECT_ATTRIBUTES attributes;
+ InitializeObjectAttributes(&attributes, &unicode_path, OBJ_CASE_INSENSITIVE, NULL,
+ NULL);
+
+ NTSTATUS status = NtDeleteFile(&attributes);
+ print_result("NtCreateFile", status);
+
+ if (!NT_SUCCESS(status))
+ throw test::FuncFailed("NtDeleteFile", status);
+}
+
+void TestNtApi::copy_file(const path& source_path, const path& destination_path,
+ bool replace_existing)
+{
+ throw test::FuncFailed("copy_file", "ntapi does not support file copy");
+}
+
+void TestNtApi::rename_file(const path& source_path, const path& destination_path,
+ bool replace_existing, bool allow_copy)
+{
+ if (allow_copy)
+ throw test::FuncFailed("rename_file", "ntapi does not support file move");
+
+ print_operation(rename_operation_name(replace_existing, allow_copy), source_path,
+ destination_path);
+
+ UNICODE_STRING unicode_path;
+ RtlInitUnicodeString(&unicode_path, source_path.c_str());
+
+ OBJECT_ATTRIBUTES attributes;
+ InitializeObjectAttributes(&attributes, &unicode_path, OBJ_CASE_INSENSITIVE, NULL,
+ NULL);
+
+ SafeHandle file(this);
+ IO_STATUS_BLOCK iosb;
+ NTSTATUS status = NtCreateFile(file, FILE_READ_ATTRIBUTES | DELETE | SYNCHRONIZE,
+ &attributes, &iosb, NULL, FILE_ATTRIBUTE_NORMAL,
+ FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
+ FILE_OPEN, FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0);
+ print_result("NtCreateFile", status);
+
+ if (!NT_SUCCESS(status))
+ throw test::FuncFailed("NtCreateFile", status);
+ if (!NT_SUCCESS(iosb.Status))
+ throw test::FuncFailed("NtCreateFile", "bad iosb.Status", iosb.Status);
+
+ bool dest_full_path = source_path.parent_path() != destination_path.parent_path();
+ std::wstring dest = dest_full_path ? destination_path : destination_path.filename();
+ std::vector<char> buf(sizeof(FILE_RENAME_INFORMATION) +
+ sizeof(wchar_t) * dest.length());
+ FILE_RENAME_INFORMATION* rename =
+ reinterpret_cast<FILE_RENAME_INFORMATION*>(buf.data());
+ rename->ReplaceIfExists = replace_existing ? TRUE : FALSE;
+ rename->FileNameLength = sizeof(wchar_t) * dest.length();
+ memcpy(&rename->FileName[0], dest.data(), sizeof(wchar_t) * dest.length());
+
+ status =
+ NtSetInformationFile(file, &iosb, rename, buf.size(), MyFileRenameInformation);
+ print_result("NtSetInformationFile", status, false,
+ dest_full_path ? "rename full path" : "rename filename");
+
+ if (!NT_SUCCESS(status))
+ throw test::FuncFailed("NtSetInformationFile", status);
+ if (!NT_SUCCESS(iosb.Status))
+ throw test::FuncFailed("NtSetInformationFile", "bad iosb.Status", iosb.Status);
+}
diff --git a/libs/usvfs/test/usvfs_test_runner/test_file_operations/test_ntapi.h b/libs/usvfs/test/usvfs_test_runner/test_file_operations/test_ntapi.h
new file mode 100644
index 0000000..fc6434e
--- /dev/null
+++ b/libs/usvfs/test/usvfs_test_runner/test_file_operations/test_ntapi.h
@@ -0,0 +1,38 @@
+#pragma once
+
+#include "test_filesystem.h"
+
+class TestNtApi : public TestFileSystem
+{
+public:
+ TestNtApi(FILE* output) : TestFileSystem(output) {}
+
+ path real_path(const char* abs_or_rel_path) override;
+
+ FileInfoList list_directory(const path& directory_path) override;
+
+ void create_path(const path& directory_path) override;
+
+ void read_file(const path& file_path) override;
+
+ void write_file(const path& file_path, const void* data, std::size_t size,
+ bool add_new_line, write_mode mode, bool rw_access = false) override;
+
+ void touch_file(const path& file_path, bool full_write_access = false) override;
+
+ void delete_file(const path& file_path) override;
+
+ void copy_file(const path& source_path, const path& destination_path,
+ bool replace_existing) override;
+
+ void rename_file(const path& source_path, const path& destination_path,
+ bool replace_existing, bool allow_copy) override;
+
+ const char* id() override;
+
+private:
+ class SafeHandle;
+
+ SafeHandle open_directory(const path& directory_path, bool create,
+ bool allow_non_existence = false, long* pstatus = nullptr);
+};
diff --git a/libs/usvfs/test/usvfs_test_runner/test_file_operations/test_ntdll_declarations.h b/libs/usvfs/test/usvfs_test_runner/test_file_operations/test_ntdll_declarations.h
new file mode 100644
index 0000000..e3799ac
--- /dev/null
+++ b/libs/usvfs/test/usvfs_test_runner/test_file_operations/test_ntdll_declarations.h
@@ -0,0 +1,157 @@
+#pragma once
+
+#define STATUS_NO_MORE_FILES 0x80000006
+#define STATUS_END_OF_FILE 0xC0000011
+#define STATUS_OBJECT_NAME_NOT_FOUND 0xC0000034
+#define STATUS_OBJECT_PATH_NOT_FOUND 0xC000003A
+
+#define FILE_WRITE_TO_END_OF_FILE 0xffffffff
+#define FILE_USE_FILE_POINTER_POSITION 0xfffffffe
+
+#define InitializeObjectAttributes(p, n, a, r, s) \
+ { \
+ (p)->Length = sizeof(OBJECT_ATTRIBUTES); \
+ (p)->RootDirectory = r; \
+ (p)->Attributes = a; \
+ (p)->ObjectName = n; \
+ (p)->SecurityDescriptor = s; \
+ (p)->SecurityQualityOfService = NULL; \
+ }
+
+// winternl.h of course defines a joke FILE_INFORMATION_CLASS (why?!)
+// so added MY_ to avoid name collision but this is FILE_INFORMATION_CLASS
+
+typedef enum _MY_FILE_INFORMATION_CLASS
+{
+ MyFileDirectoryInformation = 1,
+ MyFileFullDirectoryInformation,
+ MyFileBothDirectoryInformation,
+ MyFileBasicInformation,
+ MyFileStandardInformation,
+ MyFileInternalInformation,
+ MyFileEaInformation,
+ MyFileAccessInformation,
+ MyFileNameInformation,
+ MyFileRenameInformation,
+ MyFileLinkInformation,
+ MyFileNamesInformation,
+ MyFileDispositionInformation,
+ MyFilePositionInformation,
+ MyFileFullEaInformation,
+ MyFileModeInformation,
+ MyFileAlignmentInformation,
+ MyFileAllInformation,
+ MyFileAllocationInformation,
+ MyFileEndOfFileInformation,
+ MyFileAlternateNameInformation,
+ MyFileStreamInformation,
+ MyFilePipeInformation,
+ MyFilePipeLocalInformation,
+ MyFilePipeRemoteInformation,
+ MyFileMailslotQueryInformation,
+ MyFileMailslotSetInformation,
+ MyFileCompressionInformation,
+ MyFileObjectIdInformation,
+ MyFileCompletionInformation,
+ MyFileMoveClusterInformation,
+ MyFileQuotaInformation,
+ MyFileReparsePointInformation,
+ MyFileNetworkOpenInformation,
+ MyFileAttributeTagInformation,
+ MyFileTrackingInformation,
+ MyFileIdBothDirectoryInformation,
+ MyFileIdFullDirectoryInformation,
+ MyFileValidDataLengthInformation,
+ MyFileShortNameInformation,
+ MyFileIoCompletionNotificationInformation,
+ MyFileIoStatusBlockRangeInformation,
+ MyFileIoPriorityHintInformation,
+ MyFileSfioReserveInformation,
+ MyFileSfioVolumeInformation,
+ MyFileHardLinkInformation,
+ MyFileProcessIdsUsingFileInformation,
+ MyFileNormalizedNameInformation,
+ MyFileNetworkPhysicalNameInformation,
+ MyFileIdGlobalTxDirectoryInformation,
+ MyFileIsRemoteDeviceInformation,
+ MyFileUnusedInformation,
+ MyFileNumaNodeInformation,
+ MyFileStandardLinkInformation,
+ MyFileRemoteProtocolInformation,
+ MyFileRenameInformationBypassAccessCheck,
+ MyFileLinkInformationBypassAccessCheck,
+ MyFileVolumeNameInformation,
+ MyFileIdInformation,
+ MyFileIdExtdDirectoryInformation,
+ MyFileReplaceCompletionInformation,
+ MyFileHardLinkFullIdInformation,
+ MyFileIdExtdBothDirectoryInformation,
+ MyFileDispositionInformationEx,
+ MyFileRenameInformationEx,
+ MyFileRenameInformationExBypassAccessCheck,
+ MyFileMaximumInformation
+} MY_FILE_INFORMATION_CLASS,
+ *PMY_FILE_INFORMATION_CLASS;
+
+typedef struct _FILE_BOTH_DIR_INFORMATION
+{
+ ULONG NextEntryOffset;
+ ULONG FileIndex;
+ LARGE_INTEGER CreationTime;
+ LARGE_INTEGER LastAccessTime;
+ LARGE_INTEGER LastWriteTime;
+ LARGE_INTEGER ChangeTime;
+ LARGE_INTEGER EndOfFile;
+ LARGE_INTEGER AllocationSize;
+ ULONG FileAttributes;
+ ULONG FileNameLength;
+ ULONG EaSize;
+ CCHAR ShortNameLength;
+ WCHAR ShortName[12];
+ WCHAR FileName[1];
+} FILE_BOTH_DIR_INFORMATION, *PFILE_BOTH_DIR_INFORMATION;
+
+typedef struct _FILE_BASIC_INFORMATION
+{
+ LARGE_INTEGER CreationTime;
+ LARGE_INTEGER LastAccessTime;
+ LARGE_INTEGER LastWriteTime;
+ LARGE_INTEGER ChangeTime;
+ ULONG FileAttributes;
+} FILE_BASIC_INFORMATION, *PFILE_BASIC_INFORMATION;
+
+typedef struct _FILE_END_OF_FILE_INFORMATION
+{
+ LARGE_INTEGER EndOfFile;
+} FILE_END_OF_FILE_INFORMATION, *PFILE_END_OF_FILE_INFORMATION;
+
+typedef struct _FILE_RENAME_INFORMATION
+{
+ BOOLEAN ReplaceIfExists;
+ HANDLE RootDirectory;
+ ULONG FileNameLength;
+ WCHAR FileName[1];
+} FILE_RENAME_INFORMATION, *PFILE_RENAME_INFORMATION;
+
+extern "C" __kernel_entry NTSTATUS NTAPI NtQueryDirectoryFile(
+ IN HANDLE FileHandle, IN HANDLE Event, IN PIO_APC_ROUTINE ApcRoutine,
+ IN PVOID ApcContext, OUT PIO_STATUS_BLOCK IoStatusBlock, OUT PVOID FileInformation,
+ IN ULONG Length, IN MY_FILE_INFORMATION_CLASS FileInformationClass,
+ IN BOOLEAN ReturnSingleEntry, IN PUNICODE_STRING FileName, IN BOOLEAN RestartScan);
+
+extern "C" __kernel_entry NTSTATUS NTAPI
+NtReadFile(IN HANDLE FileHandle, IN HANDLE Event, IN PIO_APC_ROUTINE ApcRoutine,
+ IN PVOID ApcContext, OUT PIO_STATUS_BLOCK IoStatusBlock, OUT PVOID Buffer,
+ IN ULONG Length, IN PLARGE_INTEGER ByteOffset, IN PULONG Key);
+
+extern "C" __kernel_entry NTSTATUS NTAPI
+NtWriteFile(IN HANDLE FileHandle, IN HANDLE Event, IN PIO_APC_ROUTINE ApcRoutine,
+ IN PVOID ApcContext, OUT PIO_STATUS_BLOCK IoStatusBlock, IN PVOID Buffer,
+ IN ULONG Length, IN PLARGE_INTEGER ByteOffset, IN PULONG Key);
+
+extern "C" __kernel_entry NTSTATUS NTAPI NtSetInformationFile(
+ IN HANDLE FileHandle, OUT PIO_STATUS_BLOCK IoStatusBlock, IN PVOID FileInformation,
+ IN ULONG Length, IN MY_FILE_INFORMATION_CLASS FileInformationClass);
+
+extern "C" __kernel_entry NTSTATUS NTAPI
+NtDeleteFile(IN POBJECT_ATTRIBUTES ObjectAttributes);
diff --git a/libs/usvfs/test/usvfs_test_runner/test_file_operations/test_w32api.cpp b/libs/usvfs/test/usvfs_test_runner/test_file_operations/test_w32api.cpp
new file mode 100644
index 0000000..372a6f3
--- /dev/null
+++ b/libs/usvfs/test/usvfs_test_runner/test_file_operations/test_w32api.cpp
@@ -0,0 +1,370 @@
+
+#include "test_w32api.h"
+#include <cstdio>
+#include <cstring>
+#include <test_helpers.h>
+
+#define WIN32_LEAN_AND_MEAN
+#include <Windows.h>
+
+using test::throw_testWinFuncFailed;
+
+class TestW32Api::SafeHandle
+{
+public:
+ SafeHandle(TestFileSystem* tfs, HANDLE handle = NULL) : m_handle(handle), m_tfs(tfs)
+ {}
+ SafeHandle(const SafeHandle&) = delete;
+ SafeHandle(SafeHandle&& other) : m_handle(other.m_handle), m_tfs(other.m_tfs)
+ {
+ other.m_handle = nullptr;
+ }
+
+ operator HANDLE() { return m_handle; }
+ operator PHANDLE() { return &m_handle; }
+ uint32_t result_for_print()
+ {
+ return static_cast<uint32_t>(reinterpret_cast<uintptr_t>(m_handle));
+ }
+
+ bool valid() const { return m_handle != INVALID_HANDLE_VALUE; }
+
+ ~SafeHandle()
+ {
+ if (m_handle != INVALID_HANDLE_VALUE) {
+ BOOL res = CloseHandle(m_handle);
+ if (m_tfs)
+ m_tfs->print_result("CloseHandle", res, true);
+ if (!res)
+ if (m_tfs)
+ m_tfs->print_error("CloseHandle", res, true);
+ else
+ std::fprintf(stderr, "CloseHandle failed : %d", GetLastError());
+ m_handle = NULL;
+ }
+ }
+
+private:
+ HANDLE m_handle;
+ TestFileSystem* m_tfs;
+};
+
+class TestW32Api::SafeFindHandle
+{
+public:
+ SafeFindHandle(TestFileSystem* tfs, HANDLE handle = NULL)
+ : m_handle(handle), m_tfs(tfs)
+ {}
+ SafeFindHandle(const SafeFindHandle&) = delete;
+ SafeFindHandle(SafeFindHandle&& other) : m_handle(other.m_handle), m_tfs(other.m_tfs)
+ {
+ other.m_handle = nullptr;
+ }
+
+ operator HANDLE() { return m_handle; }
+ operator PHANDLE() { return &m_handle; }
+ uint32_t result_for_print()
+ {
+ return static_cast<uint32_t>(reinterpret_cast<uintptr_t>(m_handle));
+ }
+
+ bool valid() const { return m_handle != INVALID_HANDLE_VALUE; }
+
+ ~SafeFindHandle()
+ {
+ if (m_handle != INVALID_HANDLE_VALUE) {
+ BOOL res = FindClose(m_handle);
+ if (m_tfs)
+ m_tfs->print_result("CloseHandle", res, true);
+ if (!res)
+ if (m_tfs)
+ m_tfs->print_error("CloseHandle", res, true);
+ else
+ std::fprintf(stderr, "CloseHandle failed : %d", GetLastError());
+ m_handle = NULL;
+ }
+ }
+
+private:
+ HANDLE m_handle;
+ TestFileSystem* m_tfs;
+};
+
+const char* TestW32Api::id()
+{
+ return "W32";
+}
+
+TestW32Api::path TestW32Api::real_path(const char* abs_or_rel_path)
+{
+ if (!abs_or_rel_path || !abs_or_rel_path[0])
+ return path();
+
+ char buf[1024];
+ size_t res = GetFullPathNameA(abs_or_rel_path, _countof(buf), buf, NULL);
+ if (!res || res >= _countof(buf))
+ throw_testWinFuncFailed("GetFullPathNameA", res);
+ return buf;
+}
+
+TestFileSystem::FileInfoList TestW32Api::list_directory(const path& directory_path)
+{
+ print_operation("Querying directory", directory_path);
+
+ WIN32_FIND_DATA fd;
+ SafeFindHandle find(this, FindFirstFileW((directory_path / L"*").c_str(), &fd));
+ print_result("FindFirstFileW", 0, true, nullptr, true);
+ if (!find.valid())
+ throw_testWinFuncFailed("FindFirstFileW");
+
+ FileInfoList files;
+ while (true) {
+ files.push_back(
+ FileInformation(fd.cFileName, clean_attributes(fd.dwFileAttributes),
+ fd.nFileSizeHigh * (MAXDWORD + 1) + fd.nFileSizeLow));
+ BOOL res = FindNextFileW(find, &fd);
+ print_result("FindNextFileW", res, true);
+ if (!res)
+ break;
+ }
+
+ return files;
+}
+
+void TestW32Api::create_path(const path& directory_path)
+{
+ // sanity and guaranteed recursion end:
+ if (!directory_path.has_relative_path())
+ throw std::runtime_error("Refusing to create non-existing top level path");
+
+ print_operation("Checking directory", directory_path);
+
+ DWORD attr = GetFileAttributesW(directory_path.c_str());
+ DWORD err = GetLastError();
+ print_result("GetFileAttributesW", clean_attributes(attr), true);
+ if (attr != INVALID_FILE_ATTRIBUTES) {
+ if (attr & FILE_ATTRIBUTE_DIRECTORY)
+ return; // if directory already exists all is good
+ else
+ throw std::runtime_error("path exists but not a directory");
+ }
+ if (err != ERROR_FILE_NOT_FOUND && err != ERROR_PATH_NOT_FOUND)
+ throw_testWinFuncFailed("GetFileAttributesW");
+
+ if (err != ERROR_FILE_NOT_FOUND) // ERROR_FILE_NOT_FOUND means parent directory
+ // already exists
+ create_path(directory_path
+ .parent_path()); // otherwise create parent directory (recursively)
+
+ print_operation("Creating directory", directory_path);
+
+ BOOL res = CreateDirectoryW(directory_path.c_str(), NULL);
+ print_result("CreateDirectoryW", res, true);
+ if (!res)
+ throw_testWinFuncFailed("CreateDirectoryW");
+}
+
+void TestW32Api::read_file(const path& file_path)
+{
+ print_operation("Reading file", file_path);
+
+ SafeHandle file(this, CreateFileW(file_path.c_str(), GENERIC_READ, FILE_SHARE_READ,
+ NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL));
+ print_result("CreateFileW", 0, true, nullptr, true);
+ if (!file.valid())
+ throw_testWinFuncFailed("CreateFileW");
+
+ uint32_t total = 0;
+ bool ends_with_newline = true;
+ bool pending_prefix = true;
+ while (true) {
+ char buf[4096];
+
+ DWORD read = 0;
+ BOOL res = ReadFile(file, buf, sizeof(buf), &read, NULL);
+ print_result("ReadFile", res, true);
+ if (!res)
+ throw_testWinFuncFailed("ReadFile");
+ if (!read) // eof
+ break;
+
+ total += read;
+ char* begin = buf;
+ char* end = begin + read;
+ while (begin != end) {
+ if (pending_prefix) {
+ if (output())
+ fwrite(FILE_CONTENTS_PRINT_PREFIX, 1, strlen(FILE_CONTENTS_PRINT_PREFIX),
+ output());
+ pending_prefix = false;
+ }
+ bool skip_newline = false;
+ char* print_end = reinterpret_cast<char*>(std::memchr(begin, '\n', end - begin));
+ if (print_end) {
+ pending_prefix = true;
+ if (print_end > begin && *(print_end - 1) == '\r') {
+ // convert \r\n => \n:
+ *(print_end - 1) = '\n';
+ skip_newline = true;
+ } else // only a '\n' so just print it
+ ++print_end;
+ } else {
+ print_end = end;
+ if (print_end > begin && *(print_end - 1) == '\r') {
+ // buffer ends with \r so skip it under the hope it will be followed with a \n
+ --print_end;
+ skip_newline = true;
+ }
+ }
+ if (output())
+ fwrite(begin, 1, print_end - begin, output());
+ ends_with_newline = print_end > begin && *(print_end - 1) == '\n';
+ begin = print_end;
+ if (skip_newline)
+ ++begin;
+ }
+ if (output() && !ends_with_newline) {
+ fwrite("\n", 1, 1, output());
+ ends_with_newline = true;
+ }
+ }
+ if (output()) {
+ fprintf(output(), "# Successfully read %u bytes.\n", total);
+ }
+}
+
+void TestW32Api::write_file(const path& file_path, const void* data, std::size_t size,
+ bool add_new_line, write_mode mode, bool rw_access)
+{
+ print_operation(write_operation_name(mode), file_path);
+
+ ACCESS_MASK access = GENERIC_WRITE;
+ DWORD disposition = OPEN_EXISTING;
+ switch (mode) {
+ case write_mode::truncate:
+ disposition = TRUNCATE_EXISTING;
+ break;
+ case write_mode::create:
+ disposition = CREATE_NEW;
+ break;
+ case write_mode::overwrite:
+ disposition = CREATE_ALWAYS;
+ break;
+ case write_mode::opencreate:
+ disposition = OPEN_ALWAYS;
+ break;
+ case write_mode::append:
+ disposition = OPEN_ALWAYS;
+ access = FILE_APPEND_DATA;
+ break;
+ }
+ if (rw_access)
+ access |= GENERIC_READ;
+
+ SafeHandle file(this, CreateFile(file_path.c_str(), access, 0, NULL, disposition,
+ FILE_ATTRIBUTE_NORMAL, NULL));
+ print_result("CreateFileW", 0, true, nullptr, true);
+ if (!file.valid())
+ throw_testWinFuncFailed("CreateFile");
+
+ if (mode == write_mode::manual_truncate) {
+ BOOL res = SetEndOfFile(file);
+ print_result("SetEndOfFile", res, true);
+ if (!res)
+ throw_testWinFuncFailed("SetEndOfFile");
+ }
+
+ if (mode == write_mode::append) {
+ DWORD res = SetFilePointer(file, 0, NULL, FILE_END);
+ print_result("SetFilePointer(FILE_END)", res, true);
+ if (res == INVALID_SET_FILE_POINTER)
+ throw_testWinFuncFailed("SetEndOfFile");
+ }
+
+ size_t total = 0;
+
+ if (data) {
+ // finally write the data:
+ DWORD written = 0;
+ BOOL res = WriteFile(file, data, static_cast<DWORD>(size), &written, NULL);
+ print_result("WriteFile", written, true);
+ if (!res)
+ throw_testWinFuncFailed("WriteFile");
+ total += written;
+
+ if (add_new_line) {
+ res = WriteFile(file, "\r\n", 2, &written, NULL);
+ print_result("WriteFile", written, true, "<new line>");
+ if (!res)
+ throw_testWinFuncFailed("WriteFile");
+ total += written;
+ }
+ }
+
+ print_write_success(data, size, total);
+}
+
+void TestW32Api::touch_file(const path& file_path, bool full_write_access)
+{
+ print_operation("Touching file", file_path);
+
+ SYSTEMTIME st;
+ GetSystemTime(&st);
+ FILETIME ft;
+ if (!SystemTimeToFileTime(&st, &ft))
+ throw_testWinFuncFailed("SystemTimeToFileTime");
+
+ auto share_all = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
+ auto access = full_write_access ? GENERIC_WRITE : FILE_WRITE_ATTRIBUTES;
+ SafeHandle file(this, CreateFile(file_path.c_str(), access, share_all, NULL,
+ OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL));
+ print_result("CreateFileW", 0, true, nullptr, true);
+ if (!file.valid())
+ throw_testWinFuncFailed("CreateFile");
+
+ BOOL res = SetFileTime(file, nullptr, nullptr, &ft);
+ print_result("SetFileTime", res, true);
+ if (!res)
+ throw_testWinFuncFailed("SetFileTime");
+}
+
+void TestW32Api::delete_file(const path& file_path)
+{
+ print_operation("Deleting file", file_path);
+
+ BOOL res = DeleteFileW(file_path.c_str());
+ print_result("DeleteFileW", res, true);
+ if (!res)
+ throw_testWinFuncFailed("DeleteFileW");
+}
+
+void TestW32Api::copy_file(const path& source_path, const path& destination_path,
+ bool replace_existing)
+{
+ print_operation(replace_existing ? "Copy file over" : "Copy file", source_path,
+ destination_path);
+
+ BOOL res =
+ CopyFileW(source_path.c_str(), destination_path.c_str(), !replace_existing);
+ print_result("CopyFileW", res, true);
+ if (!res)
+ throw_testWinFuncFailed("CopyFileW");
+}
+
+void TestW32Api::rename_file(const path& source_path, const path& destination_path,
+ bool replace_existing, bool allow_copy)
+{
+ print_operation(rename_operation_name(replace_existing, allow_copy), source_path,
+ destination_path);
+
+ DWORD flags = 0;
+ if (replace_existing)
+ flags |= MOVEFILE_REPLACE_EXISTING;
+ if (allow_copy)
+ flags |= MOVEFILE_COPY_ALLOWED;
+
+ BOOL res = MoveFileExW(source_path.c_str(), destination_path.c_str(), flags);
+ print_result("MoveFileExW", res, true);
+ if (!res)
+ throw_testWinFuncFailed("MoveFileExW");
+}
diff --git a/libs/usvfs/test/usvfs_test_runner/test_file_operations/test_w32api.h b/libs/usvfs/test/usvfs_test_runner/test_file_operations/test_w32api.h
new file mode 100644
index 0000000..cab8e54
--- /dev/null
+++ b/libs/usvfs/test/usvfs_test_runner/test_file_operations/test_w32api.h
@@ -0,0 +1,36 @@
+#pragma once
+
+#include "test_filesystem.h"
+
+class TestW32Api : public TestFileSystem
+{
+public:
+ TestW32Api(FILE* output) : TestFileSystem(output) {}
+
+ path real_path(const char* abs_or_rel_path) override;
+
+ FileInfoList list_directory(const path& directory_path) override;
+
+ void create_path(const path& directory_path) override;
+
+ void read_file(const path& file_path) override;
+
+ void write_file(const path& file_path, const void* data, std::size_t size,
+ bool add_new_line, write_mode mode, bool rw_access = false) override;
+
+ void touch_file(const path& file_path, bool full_write_access = false) override;
+
+ void delete_file(const path& file_path) override;
+
+ void copy_file(const path& source_path, const path& destination_path,
+ bool replace_existing) override;
+
+ void rename_file(const path& source_path, const path& destination_path,
+ bool replace_existing, bool allow_copy) override;
+
+ const char* id() override;
+
+private:
+ class SafeHandle;
+ class SafeFindHandle;
+};
diff --git a/libs/usvfs/test/usvfs_test_runner/usvfs_test/usvfs_basic_test.cpp b/libs/usvfs/test/usvfs_test_runner/usvfs_test/usvfs_basic_test.cpp
new file mode 100644
index 0000000..c5fb3a1
--- /dev/null
+++ b/libs/usvfs/test/usvfs_test_runner/usvfs_test/usvfs_basic_test.cpp
@@ -0,0 +1,402 @@
+#include "usvfs_basic_test.h"
+
+const char* usvfs_basic_test::scenario_name()
+{
+ return SCENARIO_NAME;
+}
+
+bool usvfs_basic_test::scenario_run()
+{
+ // Note: For regression purposes we don't really need to verify the results of most
+ // our operations as the usvfs_test_base postmortem_check will verify the final state.
+ // We still also verify the results here because:
+ // A. In some cases a later step may change the results.
+ // B. It is easier to understand and maintain the test when the important checks are
+ // together with
+ // their related operations.
+ // C. For open issues the verifications here serve as a "documentation" of the issue
+ // (i.e. not having
+ // proper copy_on_write, etc.).
+
+ // Cases not covered by the test:
+ // - When a "temporary" virtualized directory is created (meaning the directory is
+ // created only to "shadow"
+ // a real directory) and is then emptied. This needs to be tested within the same
+ // prcoess and across different processes.
+
+ ops_list(LR"(.)", true, true);
+
+ // test proper path creation under overwrite when a virtualized folder is written to:
+
+ verify_source_existance(LR"(overwrite\mfolder1)", false);
+ verify_source_existance(LR"(overwrite\mfolder2)", false);
+ verify_source_existance(LR"(overwrite\mfolder3)", false);
+ verify_source_existance(LR"(overwrite\mfolder4)", false);
+
+ ops_overwrite(LR"(mfolder1\newfolder1\newfile1.txt)",
+ R"(newfile1.txt nonrecursive overwrite subfolder)", false);
+ verify_source_existance(LR"(overwrite\mfolder1\newfolder1\newfile1.txt)");
+ ops_overwrite(LR"(mfolder1\newfile1.txt)", R"(newfile1.txt nonrecursive overwrite)",
+ false);
+ ops_read(LR"(mfolder1\newfile1.txt)");
+
+ ops_overwrite(LR"(mfolder2\newfile2.txt)", R"(newfile2.txt recursive overwrite)",
+ true);
+ ops_read(LR"(mfolder2\newfile2.txt)");
+ verify_source_contents(LR"(overwrite\mfolder2\newfile2.txt)",
+ R"(newfile2.txt recursive overwrite)");
+ ops_overwrite(LR"(mfolder2\newfile2.txt\fail)",
+ R"(newfile2.txt is a file so folder creation should fail)", true,
+ false);
+ verify_source_contents(LR"(overwrite\mfolder2\newfile2.txt)",
+ R"(newfile2.txt recursive overwrite)");
+ ops_overwrite(LR"(mfolder2\mfile.txt\fail)",
+ R"(mfile.txt is a file so folder creation should fail)", true, false);
+ verify_source_existance(LR"(overwrite\mfolder2\mfile.txt)", false);
+
+ ops_overwrite(LR"(mfolder3\newfolder3\newfile3.txt)",
+ R"(newfile3.txt recursive overwrite)", true);
+ ops_read(LR"(mfolder3\newfolder3\newfile3.txt)");
+ verify_source_contents(LR"(overwrite\mfolder3\newfolder3\newfile3.txt)",
+ R"(newfile3.txt recursive overwrite)");
+ // repeat mfolder3\newfolder3 test as that folder now exists in overwrite and that
+ // changes things
+ ops_overwrite(LR"(mfolder3\newfolder3\newfile3e.txt)",
+ R"(newfile3e.txt recursive overwrite)", true);
+ ops_read(LR"(mfolder3\newfolder3\newfile3e.txt)");
+ verify_source_contents(LR"(overwrite\mfolder3\newfolder3\newfile3e.txt)",
+ R"(newfile3e.txt recursive overwrite)");
+
+ ops_overwrite(LR"(mfolder4\newfolder4\d\e\e\p\newfile4.txt)",
+ R"(newfile4.txt recursive overwrite)", true);
+ ops_read(LR"(mfolder4\newfolder4\d\e\e\p\newfile4.txt)");
+ verify_source_contents(LR"(overwrite\mfolder4\newfolder4\d\e\e\p\newfile4.txt)",
+ R"(newfile4.txt recursive overwrite)");
+ // repeat mfolder4\newfolder4\d\e\e\p test as that folder now exists in overwrite and
+ // that changes things
+ ops_overwrite(LR"(mfolder4\newfolder4\d\e\e\p\newfile4e.txt)",
+ R"(newfile4e.txt recursive overwrite)", true);
+ ops_read(LR"(mfolder4\newfolder4\d\e\e\p\newfile4e.txt)");
+ verify_source_contents(LR"(overwrite\mfolder4\newfolder4\d\e\e\p\newfile4e.txt)",
+ R"(newfile4e.txt recursive overwrite)");
+ // and finally verify also non-recursive works
+ ops_overwrite(LR"(mfolder4\newfolder4\d\e\e\p\newfile4enr.txt)",
+ R"(newfile4enr.txt nonrecursive overwrite)", false);
+ ops_read(LR"(mfolder4\newfolder4\d\e\e\p\newfile4enr.txt)");
+ verify_source_contents(LR"(overwrite\mfolder4\newfolder4\d\e\e\p\newfile4enr.txt)",
+ R"(newfile4enr.txt nonrecursive overwrite)");
+ // finally check an intermediate folder:
+ ops_overwrite(LR"(mfolder4\newfolder4\d\e\epnewfile4r.txt)",
+ R"(epnewfile4r.txt recursive overwrite)", true);
+ ops_read(LR"(mfolder4\newfolder4\d\e\epnewfile4r.txt)");
+ verify_source_contents(LR"(overwrite\mfolder4\newfolder4\d\e\epnewfile4r.txt)",
+ R"(epnewfile4r.txt recursive overwrite)");
+ ops_overwrite(LR"(mfolder4\newfolder4\d\e\epnewfile4.txt)",
+ R"(epnewfile4.txt nonrecursive overwrite)", false);
+ ops_read(LR"(mfolder4\newfolder4\d\e\epnewfile4.txt)");
+ verify_source_contents(LR"(overwrite\mfolder4\newfolder4\d\e\epnewfile4.txt)",
+ R"(epnewfile4.txt nonrecursive overwrite)");
+
+ verify_mount_existance(LR"(rfolder\rcopyme4.txt)");
+ verify_source_existance(LR"(overwrite\mfolder4\newfolder4p)", false);
+ ops_copy(LR"(rfolder\rcopyme4.txt)", LR"(mfolder4\newfolder4p\rcopyme4.txt)", true);
+ verify_source_existance(LR"(overwrite\mfolder4\newfolder4p\rcopyme4.txt)", true);
+ verify_source_existance(LR"(mod4\mfolder4\mfile.txt)");
+ verify_source_existance(LR"(overwrite\mfolder4\mfile.txt)", false);
+ ops_copy(LR"(rfolder\rcopyme4.txt)", LR"(mfolder4\rcopyme4.txt)", false);
+ verify_source_existance(LR"(overwrite\mfolder4\rcopyme4.txt)");
+
+ verify_source_contents(LR"(overwrite\mfolder4\newfolder4\d\e\e\p\newfile4.txt)",
+ R"(newfile4.txt recursive overwrite)");
+ // repeat mfolder4\newfolder4\d\e\e\p test as that folder now exists in overwrite and
+ // that changes things
+ ops_overwrite(LR"(mfolder4\newfolder4\d\e\e\p\newfile4e.txt)",
+ R"(newfile4e.txt recursive overwrite)", true);
+ ops_read(LR"(mfolder4\newfolder4\d\e\e\p\newfile4e.txt)");
+ verify_source_contents(LR"(overwrite\mfolder4\newfolder4\d\e\e\p\newfile4e.txt)",
+ R"(newfile4e.txt recursive overwrite)");
+ // and finally verify also non-recursive works
+ ops_overwrite(LR"(mfolder4\newfolder4\d\e\e\p\newfile4enr.txt)",
+ R"(newfile4enr.txt nonrecursive overwrite)", false);
+ ops_read(LR"(mfolder4\newfolder4\d\e\e\p\newfile4enr.txt)");
+ verify_source_contents(LR"(overwrite\mfolder4\newfolder4\d\e\e\p\newfile4enr.txt)",
+ R"(newfile4enr.txt nonrecursive overwrite)");
+ // finally check an intermediate folder:
+ ops_overwrite(LR"(mfolder4\newfolder4\d\e\epnewfile4r.txt)",
+ R"(epnewfile4r.txt recursive overwrite)", true);
+ ops_read(LR"(mfolder4\newfolder4\d\e\epnewfile4r.txt)");
+ verify_source_contents(LR"(overwrite\mfolder4\newfolder4\d\e\epnewfile4r.txt)",
+ R"(epnewfile4r.txt recursive overwrite)");
+ ops_overwrite(LR"(mfolder4\newfolder4\d\e\epnewfile4.txt)",
+ R"(epnewfile4.txt nonrecursive overwrite)", false);
+ ops_read(LR"(mfolder4\newfolder4\d\e\epnewfile4.txt)");
+ verify_source_contents(LR"(overwrite\mfolder4\newfolder4\d\e\epnewfile4.txt)",
+ R"(epnewfile4.txt nonrecursive overwrite)");
+
+ // test copy on write/delete against source "mod":
+
+ ops_touch(LR"(root0.txt)");
+ verify_source_existance(LR"(overwrite\root0.txt)", false);
+ ops_touch(LR"(root1.txt)");
+ verify_source_existance(LR"(overwrite\root1.txt)", false);
+ ops_touch(LR"(root2.txt)");
+ verify_source_existance(LR"(overwrite\root1.txt)", false);
+ ops_touch(LR"(mod1.txt)");
+ verify_source_existance(LR"(overwrite\mod1.txt)", false);
+ ops_touch(LR"(mfolder1\mfile.txt)");
+ verify_source_existance(LR"(overwrite\mfolder1\mfile.txt)", false);
+
+ ops_touch(LR"(root0w.txt)", true);
+ verify_source_existance(LR"(overwrite\root0w.txt)", false);
+ ops_touch(LR"(root1w.txt)", true);
+ verify_source_existance(LR"(overwrite\root1w.txt)", false);
+ ops_touch(LR"(root2w.txt)", true);
+ verify_source_existance(LR"(overwrite\root1w.txt)", false);
+ ops_touch(LR"(mod1w.txt)", true);
+ verify_source_existance(LR"(overwrite\mod1w.txt)", false);
+ ops_touch(LR"(mfolder1\mfilew.txt)", true);
+ verify_source_existance(LR"(overwrite\mfolder1\mfilew.txt)", false);
+
+ {
+ const auto& old_contents = source_contents(LR"(mod4\mfolder4\mfileoverwrite.txt)");
+ verify_source_contents(LR"(mod4\mfolder4\mfileoverwrite.txt)",
+ old_contents.c_str());
+ ops_overwrite(LR"(mfolder4\mfileoverwrite.txt)",
+ R"(mfolder4\mfileoverwrite.txt overwrite)", false);
+ ops_read(LR"(mfolder4\mfileoverwrite.txt)");
+ if (bool protect_virtualized = false) {
+ verify_source_contents(LR"(mod4\mfolder4\mfileoverwrite.txt)",
+ old_contents.c_str());
+ verify_source_contents(LR"(overwrite\mfolder4\mfileoverwrite.txt)",
+ R"(mfolder4\mfileoverwrite.txt overwrite)");
+ } else {
+ verify_source_contents(LR"(mod4\mfolder4\mfileoverwrite.txt)",
+ R"(mfolder4\mfileoverwrite.txt overwrite)");
+ verify_source_existance(LR"(overwrite\mfolder4\mfileoverwrite.txt)", false);
+ }
+ }
+
+ {
+ const auto& old_contents = source_contents(LR"(mod4\mfolder4\mfilerewrite.txt)");
+ verify_source_contents(LR"(mod4\mfolder4\mfilerewrite.txt)", old_contents.c_str());
+ ops_rewrite(LR"(mfolder4\mfilerewrite.txt)",
+ R"(mfolder4\mfilerewrite.txt rewrite)");
+ ops_read(LR"(mfolder4\mfilerewrite.txt)");
+ if (auto copy_on_readwrite_implemented = false) {
+ verify_source_contents(LR"(mod4\mfolder4\mfilerewrite.txt)",
+ old_contents.c_str());
+ verify_source_contents(LR"(overwrite\mfolder4\mfilerewrite.txt)",
+ R"(mfolder4\mfilerewrite.txt rewrite)");
+ } else {
+ verify_source_contents(LR"(mod4\mfolder4\mfilerewrite.txt)",
+ R"(mfolder4\mfilerewrite.txt rewrite)");
+ verify_source_existance(LR"(overwrite\mfolder4\mfilerewrite.txt)", false);
+ }
+ }
+
+ {
+ const auto& old_contents = source_contents(LR"(mod4\mfolder4\mfilemoveover.txt)");
+ verify_source_contents(LR"(mod4\mfolder4\mfilemoveover.txt)", old_contents.c_str());
+ ops_overwrite(LR"(mfolder4\temp_mfilemoveover.txt)",
+ R"(mfolder4\mfilemoveover.txt overwrite)", false);
+ verify_source_contents(LR"(overwrite\mfolder4\temp_mfilemoveover.txt)",
+ R"(mfolder4\mfilemoveover.txt overwrite)");
+ ops_rename(LR"(mfolder4\temp_mfilemoveover.txt)", LR"(mfolder4\mfilemoveover.txt)",
+ true);
+ ops_read(LR"(mfolder4\mfilemoveover.txt)");
+ verify_source_existance(LR"(overwrite\mfolder4\temp_mfilemoveover.txt)", false);
+ if (bool protect_virtualized = false) {
+ verify_source_contents(LR"(mod4\mfolder4\mfilemoveover.txt)",
+ old_contents.c_str());
+ verify_source_contents(LR"(overwrite\mfolder4\mfilemoveover.txt)",
+ R"(mfolder4\mfilemoveover.txt overwrite)");
+ } else {
+ verify_source_contents(LR"(mod4\mfolder4\mfilemoveover.txt)",
+ R"(mfolder4\mfilemoveover.txt overwrite)");
+ verify_source_existance(LR"(overwrite\mfolder4\mfilemoveover.txt)", false);
+ }
+ }
+
+ {
+ const auto& old_contents =
+ source_contents(LR"(mod4\mfolder4\mfiledeletewrite.txt)");
+ verify_source_contents(LR"(mod4\mfolder4\mfiledeletewrite.txt)",
+ old_contents.c_str());
+ ops_deleteoverwrite(LR"(mfolder4\mfiledeletewrite.txt)",
+ R"(mfolder4\mfiledeletewrite.txt overwrite)", false);
+ ops_read(LR"(mfolder4\mfiledeletewrite.txt)");
+ if (bool protect_virtualized = false) {
+ verify_source_contents(LR"(overwrite\mfolder4\mfiledeletewrite.txt)",
+ R"(mfolder4\mfiledeletewrite.txt overwrite)");
+ if (auto proper_delete_implemented = false)
+ verify_source_contents(LR"(mod4\mfolder4\mfiledeletewrite.txt)",
+ old_contents.c_str());
+ else
+ verify_source_existance(LR"(mod4\mfolder4\mfiledeletewrite.txt)", false);
+ } else {
+ verify_source_contents(LR"(mod4\mfolder4\mfiledeletewrite.txt)",
+ R"(mfolder4\mfiledeletewrite.txt overwrite)");
+ verify_source_existance(LR"(overwrite\mfolder4\mfiledeletewrite.txt)", false);
+ }
+ }
+
+ {
+ const auto& old_contents =
+ source_contents(LR"(mod4\mfolder4\mfiledeletewrite2p.txt)");
+ verify_source_contents(LR"(mod4\mfolder4\mfiledeletewrite2p.txt)",
+ old_contents.c_str());
+ ops_delete(LR"(mfolder4\mfiledeletewrite2p.txt)");
+ ops_overwrite(LR"(mfolder4\mfiledeletewrite2p.txt)",
+ R"(mfolder4\mfiledeletewrite2p.txt overwrite)", false);
+ ops_read(LR"(mfolder4\mfiledeletewrite2p.txt)");
+ if (bool protect_virtualized_or_track_deleted_only_in_current_process = true) {
+ verify_source_contents(LR"(overwrite\mfolder4\mfiledeletewrite2p.txt)",
+ R"(mfolder4\mfiledeletewrite2p.txt overwrite)");
+ if (auto proper_delete_implemented = false)
+ verify_source_contents(LR"(mod4\mfolder4\mfiledeletewrite2p.txt)",
+ old_contents.c_str());
+ else
+ verify_source_existance(LR"(mod4\mfolder4\mfiledeletewrite2p.txt)", false);
+ } else {
+ verify_source_contents(LR"(mod4\mfolder4\mfiledeletewrite2p.txt)",
+ R"(mfolder4\mfiledeletewrite2p.txt overwrite)");
+ verify_source_existance(LR"(overwrite\mfolder4\mfiledeletewrite2p.txt)", false);
+ }
+ }
+
+ {
+ const auto& old_contents = source_contents(LR"(mod4\mfolder4\mfiledeletemove.txt)");
+ verify_source_contents(LR"(mod4\mfolder4\mfiledeletemove.txt)",
+ old_contents.c_str());
+ ops_overwrite(LR"(mfolder4\temp_mfiledeletemove.txt)",
+ R"(mfolder4\mfiledeletemove.txt overwrite)", false);
+ verify_source_contents(LR"(overwrite\mfolder4\temp_mfiledeletemove.txt)",
+ R"(mfolder4\mfiledeletemove.txt overwrite)");
+ ops_deleterename(LR"(mfolder4\temp_mfiledeletemove.txt)",
+ LR"(mfolder4\mfiledeletemove.txt)");
+ ops_read(LR"(mfolder4\mfiledeletemove.txt)");
+ verify_source_existance(LR"(overwrite\mfolder4\temp_mfiledeletemove.txt)", false);
+ if (bool protect_virtualized = false) {
+ verify_source_contents(LR"(overwrite\mfolder4\mfiledeletemove.txt)",
+ R"(mfolder4\mfiledeletemove.txt overwrite)");
+ if (auto proper_delete_implemented = false)
+ verify_source_contents(LR"(mod4\mfolder4\mfiledeletemove.txt)",
+ old_contents.c_str());
+ else
+ verify_source_existance(LR"(mod4\mfolder4\mfiledeletemove.txt)", false);
+ } else {
+ verify_source_contents(LR"(mod4\mfolder4\mfiledeletemove.txt)",
+ R"(mfolder4\mfiledeletemove.txt overwrite)");
+ verify_source_existance(LR"(overwrite\mfolder4\mfiledeletemove.txt)", false);
+ }
+ }
+
+ {
+ const auto& old_contents =
+ source_contents(LR"(mod4\mfolder4\mfiledeletemove2p.txt)");
+ verify_source_contents(LR"(mod4\mfolder4\mfiledeletemove2p.txt)",
+ old_contents.c_str());
+ ops_delete(LR"(mfolder4\mfiledeletemove2p.txt)");
+ ops_overwrite(LR"(mfolder4\temp_mfiledeletemove2p.txt)",
+ R"(mfolder4\mfiledeletemove2p.txt overwrite)", false);
+ verify_source_contents(LR"(overwrite\mfolder4\temp_mfiledeletemove2p.txt)",
+ R"(mfolder4\mfiledeletemove2p.txt overwrite)");
+ ops_rename(LR"(mfolder4\temp_mfiledeletemove2p.txt)",
+ LR"(mfolder4\mfiledeletemove2p.txt)", false);
+ ops_read(LR"(mfolder4\mfiledeletemove2p.txt)");
+ verify_source_existance(LR"(overwrite\mfolder4\temp_mfiledeletemove2p.txt)", false);
+ if (bool protect_virtualized_or_track_deleted_only_in_current_process = true) {
+ verify_source_contents(LR"(overwrite\mfolder4\mfiledeletemove2p.txt)",
+ R"(mfolder4\mfiledeletemove2p.txt overwrite)");
+ if (auto proper_delete_implemented = false)
+ verify_source_contents(LR"(mod4\mfolder4\mfiledeletemove2p.txt)",
+ old_contents.c_str());
+ else
+ verify_source_existance(LR"(mod4\mfolder4\mfiledeletemove2p.txt)", false);
+ } else {
+ verify_source_contents(LR"(mod4\mfolder4\mfiledeletemove2p.txt)",
+ R"(mfolder4\mfiledeletemove2p.txt overwrite)");
+ verify_source_existance(LR"(overwrite\mfolder4\mfiledeletemove2p.txt)", false);
+ }
+ }
+
+ // test copy on write/delete/move against original mount files:
+
+ {
+ const auto& old_contents = mount_contents(LR"(rfolder\rfilerewrite.txt)");
+ ops_rewrite(LR"(rfolder\rfilerewrite.txt)", R"(rfolder\rfilerewrite.txt rewrite)");
+ ops_read(LR"(rfolder\rfilerewrite.txt)");
+ if (auto copy_on_readwrite_implemented = false) {
+ verify_mount_contents(LR"(rfolder\rfilerewrite.txt)", old_contents.c_str());
+ verify_source_contents(LR"(overwrite\rfolder\rfilerewrite.txt)",
+ R"(rfolder\rfilerewrite.txt rewrite)");
+ } else {
+ verify_mount_contents(LR"(rfolder\rfilerewrite.txt)",
+ R"(rfolder\rfilerewrite.txt rewrite)");
+ verify_source_existance(LR"(overwrite\rfolder\rfilerewrite.txt)", false);
+ }
+ }
+ ops_overwrite(LR"(rfolder\rfilerewrite.txt\fail)",
+ R"(rfilerewrite.txt is a file so folder creation should fail)", true,
+ false);
+ verify_mount_existance(
+ LR"(rfolder\rfilerewrite.txt)"); // verifies its a file and not a directory
+ if (auto copy_on_readwrite_implemented = false)
+ verify_source_existance(
+ LR"(overwrite\rfolder\rfilerewrite.txt)"); // verifies its a file and not a
+ // directory
+ else
+ verify_source_existance(LR"(overwrite\rfolder\rfilerewrite.txt)", false);
+ ops_overwrite(LR"(rfolder\rfile0.txt\fail)",
+ R"(rfile0.txt is a file so folder creation should fail)", true, false);
+ verify_mount_existance(
+ LR"(rfolder\rfile0.txt)"); // verifies its a file and not a directory
+
+ {
+ const auto& old_contents = mount_contents(LR"(rfolder\rfiledeletewrite.txt)");
+ ops_deleteoverwrite(LR"(rfolder\rfiledeletewrite.txt)",
+ R"(rfolder\rfiledeletewrite.txt overwrite)", false);
+ ops_read(LR"(rfolder\rfiledeletewrite.txt)");
+ if (bool protect_virtualized = false) {
+ verify_source_contents(LR"(overwrite\rfolder\rfiledeletewrite.txt)",
+ R"(rfolder\rfiledeletewrite.txt overwrite)");
+ if (auto proper_delete_implemented = false)
+ verify_mount_contents(LR"(rfolder\rfiledeletewrite.txt)", old_contents.c_str());
+ else
+ verify_mount_existance(LR"(rfolder\rfiledeletewrite.txt)", false);
+ } else {
+ verify_mount_contents(LR"(rfolder\rfiledeletewrite.txt)",
+ R"(rfolder\rfiledeletewrite.txt overwrite)");
+ verify_source_existance(LR"(overwrite\rfolder\rfiledeletewrite.txt)", false);
+ }
+ }
+
+ {
+ const auto& old_contents = mount_contents(LR"(rfolder\rfiledelete.txt)");
+ ops_delete(LR"(rfolder\rfiledelete.txt)");
+ ops_read(LR"(rfolder\rfiledelete.txt)", false);
+ if (auto proper_delete_implemented = false)
+ verify_mount_contents(LR"(rfolder\rfiledelete.txt)", old_contents.c_str());
+ else
+ verify_mount_existance(LR"(rfolder\rfiledelete.txt)", false);
+ }
+
+ {
+ const auto& old_contents = mount_contents(LR"(rfolder\rfileoldname.txt)");
+ ops_rename(LR"(rfolder\rfileoldname.txt)", LR"(rfolder\rfilenewname.txt)", false,
+ false);
+ ops_read(LR"(rfolder\rfileoldname.txt)", false);
+ ops_read(LR"(rfolder\rfilenewname.txt)");
+ verify_source_contents(LR"(overwrite\rfolder\rfilenewname.txt)",
+ old_contents.c_str());
+ verify_mount_existance(LR"(rfolder\rfilenewname.txt)", false);
+ if (auto copy_on_move_implemented = false)
+ verify_mount_contents(LR"(rfolder\rfileoldname.txt)", old_contents.c_str());
+ else
+ verify_mount_existance(LR"(rfolder\rfileoldname.txt)", false);
+ }
+
+ ops_list(LR"(.)", true, true);
+
+ return true;
+}
diff --git a/libs/usvfs/test/usvfs_test_runner/usvfs_test/usvfs_basic_test.h b/libs/usvfs/test/usvfs_test_runner/usvfs_test/usvfs_basic_test.h
new file mode 100644
index 0000000..bec81f2
--- /dev/null
+++ b/libs/usvfs/test/usvfs_test_runner/usvfs_test/usvfs_basic_test.h
@@ -0,0 +1,14 @@
+#pragma once
+
+#include "usvfs_test_base.h"
+
+class usvfs_basic_test : public usvfs_test_base
+{
+public:
+ static constexpr auto SCENARIO_NAME = "basic";
+
+ usvfs_basic_test(const usvfs_test_options& options) : usvfs_test_base(options) {}
+
+ virtual const char* scenario_name();
+ virtual bool scenario_run();
+};
diff --git a/libs/usvfs/test/usvfs_test_runner/usvfs_test/usvfs_test.cpp b/libs/usvfs/test/usvfs_test_runner/usvfs_test/usvfs_test.cpp
new file mode 100644
index 0000000..e6e6bb1
--- /dev/null
+++ b/libs/usvfs/test/usvfs_test_runner/usvfs_test/usvfs_test.cpp
@@ -0,0 +1,242 @@
+
+#include <iostream>
+#include <memory>
+
+#include <boost/filesystem.hpp>
+#include <boost/type_traits.hpp>
+#include <winapi.h>
+
+#include "usvfs_basic_test.h"
+#include <stringcast.h>
+
+void print_usage(const std::wstring& exe_name, const std::wstring& test_name)
+{
+ using namespace std;
+ wcerr << "usage: " << exe_name << " [<options>] <scenario>[:<label>]" << endl;
+ wcerr << "available options:" << endl;
+ wcerr << " -ops32 : force 32bit file opertations process (default is same "
+ "bitness)."
+ << endl;
+ wcerr << " -ops64 : force 64bit file opertations process (default is same "
+ "bitness)."
+ << endl;
+ wcerr << " -opsexe <file> : full path to file operations executable (overrides "
+ "-ops32/64)."
+ << endl;
+ wcerr << " -opsarg <arg> : adds argument to the start of all file operations."
+ << endl;
+ wcerr << " -fixture <dir> : fixture dir (default is test\\fixtures\\" << test_name
+ << "\\<scenario>)." << endl;
+ wcerr << " -mapping <file> : mapping file (default is <fixture "
+ "dir>\\vfs_mappings.txt)."
+ << endl;
+ wcerr << " -temp <dir> : temp dir (default is test\\temp\\" << test_name
+ << "\\<scenario>_<label>)." << endl;
+ wcerr << " -mount <dir> : mount dir (default is <temp dir>\\mount)." << endl;
+ wcerr << " -source <dir> : source dir (default is <temp dir>\\source)." << endl;
+ wcerr << " -out <file> : output file (default is <temp "
+ "dir>\\<scenario>_<label>.log)."
+ << endl;
+ wcerr << " -usvfslog <file> : output file (default is <temp "
+ "dir>\\<scenario>_<label>_usvfs.log)."
+ << endl;
+ wcerr << " -forcetemprecursivedelete : decimate temp dir even if doesn't look like a "
+ "temp dir."
+ << endl;
+ wcerr << endl;
+ wcerr << "note: if <label> is ommited the current platform (x86/x64) is used."
+ << endl;
+ wcerr << "note: mount and source dirs should not exist or be empty directories. if "
+ "either of them"
+ << endl;
+ wcerr << "is not given, the temp dir is first recusrively deleted. to protect "
+ "against accidents a"
+ << endl;
+ wcerr << "heuristic is used to verify the temp dir only has entires which this test "
+ "creates."
+ << endl;
+ wcerr << "-forcetemprecursivedelete can be used to circumvent this heuristic."
+ << endl;
+}
+
+usvfs_test_base* find_scenario(const std::string& scenario,
+ const usvfs_test_options& options)
+{
+ if (scenario == usvfs_basic_test::SCENARIO_NAME)
+ return new usvfs_basic_test(options);
+ else
+ return nullptr;
+}
+
+bool verify_args_exist(const wchar_t* flag, int params, int index, int count)
+{
+ if (index + params >= count) {
+ std::wcerr << flag << L" requires " << params << L" arguments" << std::endl;
+ return false;
+ } else
+ return true;
+}
+
+bool verify_file(const test::path& file)
+{
+ bool is_dir = false;
+ if (!winapi::ex::wide::fileExists(file.c_str(), &is_dir) || is_dir) {
+ std::wcerr << L"File does not exist: " << file << std::endl;
+ return false;
+ } else
+ return true;
+}
+
+bool verify_dir(const test::path& dir)
+{
+ bool is_dir = false;
+ if (!winapi::ex::wide::fileExists(dir.c_str(), &is_dir) || !is_dir) {
+ std::wcerr << L"Directory does not exist: " << dir << std::endl;
+ return false;
+ } else
+ return true;
+}
+
+std::wstring get_env_var(const wchar_t* var)
+{
+ using namespace std;
+ size_t size = GetEnvironmentVariableW(var, nullptr, 0);
+ if (size) {
+ std::wstring value(size, L'\0');
+ size = GetEnvironmentVariableW(var, &value[0], static_cast<DWORD>(value.size()));
+ if (size && size < value.size()) {
+ value.resize(size);
+ return value;
+ }
+ }
+ wcerr << L"GetEnvironmentVariableW(" << var << L") failed result=" << size
+ << L" error=" << GetLastError() << endl;
+ return wstring();
+}
+
+bool set_env_var(const wchar_t* var, const std::wstring& value)
+{
+ if (SetEnvironmentVariableW(var, value.c_str()))
+ return true;
+ std::wcerr << L"SetEnvironmentVariableW(" << var << L"," << value
+ << L") failed error=" << GetLastError() << std::endl;
+ return false;
+}
+
+int wmain(int argc, wchar_t* argv[])
+{
+ using namespace std;
+ using namespace test;
+
+ const wchar_t* label = nullptr;
+ wstring scenario;
+ path temp;
+ usvfs_test_options options;
+
+ for (int ai = 1; ai < argc; ++ai) {
+ if (wcscmp(argv[ai], L"-ops32") == 0)
+ options.set_ops32();
+ else if (wcscmp(argv[ai], L"-ops64") == 0)
+ options.set_ops64();
+ else if (wcscmp(argv[ai], L"-opsexe") == 0) {
+ if (!verify_args_exist(L"-opsexe", 1, ai, argc))
+ return 1;
+ options.opsexe = argv[++ai];
+ if (!verify_file(options.opsexe))
+ return 1;
+ } else if (wcscmp(argv[ai], L"-opsarg") == 0) {
+ if (!verify_args_exist(L"-opsarg", 1, ai, argc))
+ return 1;
+ options.add_ops_options(argv[++ai]);
+ } else if (wcscmp(argv[ai], L"-fixture") == 0) {
+ if (!verify_args_exist(L"-fixture", 1, ai, argc))
+ return 1;
+ options.fixture = argv[++ai];
+ if (!verify_dir(options.fixture))
+ return 1;
+ } else if (wcscmp(argv[ai], L"-mapping") == 0) {
+ if (!verify_args_exist(L"-mapping", 1, ai, argc))
+ return 1;
+ options.mapping = argv[++ai];
+ if (!verify_file(options.mapping))
+ return 1;
+ } else if (wcscmp(argv[ai], L"-temp") == 0) {
+ if (!verify_args_exist(L"-temp", 1, ai, argc))
+ return 1;
+ options.mount = argv[++ai];
+ } else if (wcscmp(argv[ai], L"-mount") == 0) {
+ if (!verify_args_exist(L"-mount", 1, ai, argc))
+ return 1;
+ options.mount = argv[++ai];
+ } else if (wcscmp(argv[ai], L"-source") == 0) {
+ if (!verify_args_exist(L"-source", 1, ai, argc))
+ return 1;
+ options.source = argv[++ai];
+ } else if (wcscmp(argv[ai], L"-out") == 0) {
+ if (!verify_args_exist(L"-out", 1, ai, argc))
+ return 1;
+ options.output = argv[++ai];
+ } else if (wcscmp(argv[ai], L"-usvfslog") == 0) {
+ if (!verify_args_exist(L"-usvfslog", 1, ai, argc))
+ return 1;
+ options.usvfs_log = argv[++ai];
+ } else if (wcscmp(argv[ai], L"-forcetemprecursivedelete") == 0)
+ options.force_temp_cleanup = true;
+ else if (argv[ai][0] == '-') {
+ wcerr << L"Unknown option " << argv[ai] << endl;
+ return 1;
+ } else if (!scenario.empty()) {
+ wcerr << L"Multiple scenarios can not be specified: " << scenario.c_str() << L", "
+ << argv[ai] << endl;
+ return 1;
+ } else {
+ label = wcschr(argv[ai], ':');
+ if (label) {
+ scenario = std::wstring(static_cast<const wchar_t*>(argv[ai]), label);
+ ++label; // skip the ':'
+ } else
+ scenario = argv[ai];
+
+ if (scenario.empty()) {
+ wcerr << L"Scenario name can not be empty!" << endl;
+ return 1;
+ }
+ }
+ }
+
+ path test_name{"usvfs_test"};
+
+ if (scenario.empty()) {
+ print_usage(path(argv[0]).stem(), test_name);
+ return 1;
+ }
+
+ options.fill_defaults(test_name, scenario, label);
+
+ unique_ptr<usvfs_test_base> test{find_scenario(
+ usvfs::shared::string_cast<std::string>(scenario, usvfs::shared::CodePage::UTF8),
+ options)};
+ if (!test) {
+ wcerr << L"ERROR: Unknown scenario specified: " << scenario.c_str() << endl;
+ return 2;
+ }
+
+ auto dllPath = path_of_usvfs_lib(platform_dependant_executable("usvfs", "dll"));
+ ScopedLoadLibrary loadDll(dllPath.c_str());
+ if (!loadDll) {
+ wcerr << L"ERROR: failed to load usvfs dll: " << dllPath.c_str() << L", "
+ << GetLastError() << endl;
+ return 3;
+ }
+
+ // In order for bin\usvfs_proxy_x??.exe to find the lib\usvfs_x??.dll file we add the
+ // lib folder to our path:
+ std::wstring env_path = get_env_var(L"Path");
+ if (env_path.empty() ||
+ !set_env_var(L"Path", dllPath.parent_path().wstring() + L";" + env_path)) {
+ wcerr << L"ERROR: failed to add usvfs dll directory to path" << endl;
+ return 3;
+ }
+
+ return test->run(path(argv[0]).stem());
+}
diff --git a/libs/usvfs/test/usvfs_test_runner/usvfs_test/usvfs_test_base.cpp b/libs/usvfs/test/usvfs_test_runner/usvfs_test/usvfs_test_base.cpp
new file mode 100644
index 0000000..bfa391e
--- /dev/null
+++ b/libs/usvfs/test/usvfs_test_runner/usvfs_test/usvfs_test_base.cpp
@@ -0,0 +1,1002 @@
+
+#include "usvfs_test_base.h"
+#include <boost/filesystem.hpp>
+#include <boost/type_traits.hpp>
+#include <cctype>
+#include <cerrno>
+#include <chrono>
+#include <future>
+#include <iostream>
+#include <stringcast.h>
+#include <thread>
+#include <unordered_set>
+#include <usvfs.h>
+#include <vector>
+#include <winapi.h>
+
+using test::throw_testWinFuncFailed;
+
+// usvfs_test_options class:
+
+void usvfs_test_options::fill_defaults(const path& test_name,
+ const std::wstring& scenario,
+ const wchar_t* label)
+{
+ using namespace test;
+
+ std::wstring lbl;
+ if (label)
+ lbl = label;
+
+#ifdef _WIN64
+ set_ops64();
+ if (!label)
+ lbl = L"x64";
+#else
+ set_ops32();
+ if (!label)
+ lbl = L"x86";
+#endif
+
+ std::wstring scenario_label = scenario;
+ if (!lbl.empty()) {
+ scenario_label += L"_";
+ scenario_label += lbl;
+ }
+
+ if (fixture.empty())
+ fixture = path_of_test_fixtures(test_name / scenario);
+
+ if (mapping.empty())
+ mapping = fixture / DEFAULT_MAPPING;
+
+ if (temp.empty()) {
+ temp = path_of_test_temp(test_name / scenario_label);
+ }
+
+ if (mount.empty()) {
+ mount = temp / MOUNT_DIR;
+ temp_cleanup = true;
+ }
+
+ if (source.empty()) {
+ source = temp / SOURCE_DIR;
+ temp_cleanup = true;
+ }
+
+ if (output.empty()) {
+ output = temp / scenario_label;
+ output += ".log";
+ }
+
+ if (usvfs_log.empty()) {
+ usvfs_log = temp / scenario_label;
+ usvfs_log += "_usvfs.log";
+ }
+}
+
+void usvfs_test_options::set_ops32()
+{
+ if (opsexe.empty())
+ opsexe = test::path_of_test_bin(L"test_file_operations_x86.exe");
+}
+
+void usvfs_test_options::set_ops64()
+{
+ if (opsexe.empty())
+ opsexe = test::path_of_test_bin(L"test_file_operations_x64.exe");
+}
+
+void usvfs_test_options::add_ops_options(const std::wstring& options)
+{
+ if (!ops_options.empty())
+ ops_options += L" ";
+ ops_options += options;
+}
+
+// usvfs_connector helper class:
+
+class usvfs_connector
+{
+public:
+ using path = test::path;
+
+ usvfs_connector(const usvfs_test_options& options)
+ : m_usvfs_log(test::ScopedFILE::open(options.usvfs_log, L"wt")),
+ m_exit_future(m_exit_signal.get_future())
+ {
+ winapi::ex::wide::createPath(options.usvfs_log.parent_path().c_str());
+
+ std::wcout << "Connecting VFS..." << std::endl;
+
+ std::unique_ptr<usvfsParameters, decltype(&usvfsFreeParameters)> parameters{
+ usvfsCreateParameters(), &usvfsFreeParameters};
+
+ usvfsSetInstanceName(parameters.get(), "usvfs_test");
+ usvfsSetDebugMode(parameters.get(), false);
+ usvfsSetLogLevel(parameters.get(), LogLevel::Debug);
+ usvfsSetCrashDumpType(parameters.get(), CrashDumpsType::None);
+ usvfsSetCrashDumpPath(parameters.get(), "");
+
+ usvfsInitLogging(false);
+ usvfsCreateVFS(parameters.get());
+
+ m_log_thread = std::thread(&usvfs_connector::usvfs_logger, this);
+ }
+
+ ~usvfs_connector()
+ {
+ usvfsDisconnectVFS();
+ m_exit_signal.set_value();
+ m_log_thread.join();
+ }
+
+ enum class map_type
+ {
+ none, // the mapping_reader uses this value but will never pass it to the
+ // usvfs_connector (which ignored such entries)
+ dir,
+ dircreate,
+ file
+ };
+
+ struct mapping
+ {
+ mapping(map_type itype, std::wstring isource, std::wstring idestination)
+ : type(itype), source(isource), destination(idestination)
+ {}
+
+ map_type type;
+ path source;
+ path destination;
+ };
+
+ using mappings_list = std::vector<mapping>;
+
+ void updateMapping(const mappings_list& mappings, const usvfs_test_options& options,
+ FILE* log)
+ {
+ using namespace std;
+ using namespace usvfs::shared;
+
+ fprintf(log, "Updating VFS mappings:\n");
+
+ usvfsClearVirtualMappings();
+
+ for (const auto& map : mappings) {
+ const string& source =
+ usvfs_test_base::SOURCE_LABEL +
+ test::path_as_relative(options.source, map.source).string();
+ const string& destination =
+ usvfs_test_base::MOUNT_LABEL +
+ test::path_as_relative(options.mount, map.destination).string();
+ switch (map.type) {
+ case map_type::dir:
+ fprintf(log, " mapdir: %s => %s\n", source.c_str(), destination.c_str());
+ usvfsVirtualLinkDirectoryStatic(map.source.c_str(), map.destination.c_str(),
+ LINKFLAG_RECURSIVE);
+ break;
+
+ case map_type::dircreate:
+ fprintf(log, " mapdircreate: %s => %s\n", source.c_str(), destination.c_str());
+ usvfsVirtualLinkDirectoryStatic(map.source.c_str(), map.destination.c_str(),
+ LINKFLAG_CREATETARGET | LINKFLAG_RECURSIVE);
+ break;
+
+ case map_type::file:
+ fprintf(log, " mapfile: %s => %s\n", source.c_str(), destination.c_str());
+ usvfsVirtualLinkFile(map.source.c_str(), map.destination.c_str(),
+ LINKFLAG_RECURSIVE);
+ break;
+ }
+ }
+
+ fprintf(log, "\n");
+ }
+
+ static DWORD spawn(wchar_t* commandline)
+ {
+ using namespace usvfs::shared;
+
+ STARTUPINFO si{0};
+ si.cb = sizeof(si);
+ PROCESS_INFORMATION pi{0};
+
+ if (!usvfsCreateProcessHooked(NULL, commandline, NULL, NULL, FALSE, 0, NULL, NULL,
+ &si, &pi))
+ throw_testWinFuncFailed(
+ "CreateProcessHooked",
+ string_cast<std::string>(commandline, CodePage::UTF8).c_str());
+
+ WaitForSingleObject(pi.hProcess, INFINITE);
+
+ DWORD exit = 99;
+ if (!GetExitCodeProcess(pi.hProcess, &exit)) {
+ test::WinFuncFailedGenerator failed;
+ CloseHandle(pi.hProcess);
+ CloseHandle(pi.hThread);
+ throw failed("GetExitCodeProcess");
+ }
+
+ CloseHandle(pi.hProcess);
+ CloseHandle(pi.hThread);
+
+ return exit;
+ }
+
+ void usvfs_logger()
+ {
+ fprintf(m_usvfs_log, "usvfs_test usvfs logger started:\n");
+ fflush(m_usvfs_log);
+
+ constexpr size_t size = 1024;
+ char buf[size + 1]{0};
+ int noLogCycles = 0;
+ std::chrono::milliseconds wait_for;
+ do {
+ if (usvfsGetLogMessages(buf, size, false)) {
+ fwrite(buf, 1, strlen(buf), m_usvfs_log);
+ fwrite("\n", 1, 1, m_usvfs_log);
+ fflush(m_usvfs_log);
+ noLogCycles = 0;
+ } else
+ ++noLogCycles;
+ if (noLogCycles)
+ wait_for = std::chrono::milliseconds(std::min(40, noLogCycles) * 5);
+ else
+ wait_for = std::chrono::milliseconds(0);
+ } while (m_exit_future.wait_for(wait_for) == std::future_status::timeout);
+
+ while (usvfsGetLogMessages(buf, size, false)) {
+ fwrite(buf, 1, strlen(buf), m_usvfs_log);
+ fwrite("\n", 1, 1, m_usvfs_log);
+ }
+
+ fprintf(m_usvfs_log, "usvfs log closed.\n");
+ m_usvfs_log.close();
+ }
+
+private:
+ test::ScopedFILE m_usvfs_log;
+ std::thread m_log_thread;
+ std::promise<void> m_exit_signal;
+ std::shared_future<void> m_exit_future;
+};
+
+// mappings_reader
+
+class mappings_reader
+{
+public:
+ using path = test::path;
+ using string = std::string;
+ using wstring = std::wstring;
+ using map_type = usvfs_connector::map_type;
+ using mapping = usvfs_connector::mapping;
+ using mappings_list = usvfs_connector::mappings_list;
+
+ mappings_reader(const path& mount_base, const path& source_base)
+ : m_mount_base(mount_base), m_source_base(source_base)
+ {}
+
+ mappings_list read(const path& mapfile)
+ {
+ const auto map = test::ScopedFILE::open(mapfile, L"rt");
+ mappings_list mappings;
+
+ char line[1024];
+ while (!feof(map)) {
+ // read one line:
+ if (!fgets(line, _countof(line), map))
+ if (feof(map))
+ break;
+ else
+ throw_testWinFuncFailed("fgets", "reading mappings");
+
+ if (empty_line(line))
+ continue;
+
+ if (start_nesting(line, "mapdir"))
+ m_nesting = map_type::dir;
+ else if (start_nesting(line, "mapdircreate"))
+ m_nesting = map_type::dircreate;
+ else if (start_nesting(line, "mapfile"))
+ m_nesting = map_type::file;
+ else if (!isspace(*line)) // mapping sources should be indented and we already
+ // check all the mapping directives
+ throw test::FuncFailed("mappings_reader::read", "invalid mappings file line",
+ line);
+ else {
+ const auto& source_rel = trimmed_wide_string(line);
+ mappings.push_back(mapping(m_nesting, m_source_base / source_rel, m_mount));
+ }
+ }
+
+ return mappings;
+ }
+
+ bool start_nesting(const char* line, const char* directive)
+ {
+ // check if line starts with directive and if so skip it:
+ auto dlen = strlen(directive);
+ auto after = line + dlen;
+ if (strncmp(directive, line, dlen) == 0 && (!*after || isspace(*after))) {
+ m_mount = m_mount_base;
+ const auto& mount_rel = trimmed_wide_string(after);
+ if (!mount_rel.empty())
+ m_mount /= mount_rel;
+ return true;
+ } else
+ return false;
+ }
+
+ static wstring trimmed_wide_string(const char* in)
+ {
+ while (std::isspace(*in))
+ ++in;
+ auto end = in;
+ end += strlen(end);
+ while (end > in && std::isspace(*(end - 1)))
+ --end;
+ return usvfs::shared::string_cast<wstring>(string(in, end),
+ usvfs::shared::CodePage::UTF8);
+ }
+
+ static bool empty_line(const char* line)
+ {
+ for (; *line; ++line) {
+ if (*line == '#') // comment, ignore rest of line
+ return true;
+ else if (!std::isspace(*line))
+ return false;
+ }
+ return true;
+ }
+
+private:
+ path m_mount_base;
+ path m_source_base;
+ path m_mount;
+ map_type m_nesting = map_type::none;
+};
+
+// usvfs_test_base class:
+
+void usvfs_test_base::cleanup_temp()
+{
+ using namespace test;
+ using namespace winapi::ex::wide;
+
+ bool isDir = false;
+ if (!m_o.temp_cleanup || !fileExists(m_o.temp.c_str(), &isDir))
+ return;
+
+ if (!isDir) {
+ if (m_o.force_temp_cleanup)
+ delete_file(m_o.temp);
+ else
+ throw FuncFailed("cleanup_temp", "temp exists but is a file",
+ m_o.temp.string().c_str());
+ } else {
+ std::vector<wstring> cleanfiles;
+ std::vector<wstring> cleandirs;
+ std::vector<wstring> otherdirs;
+ bool output_file = false;
+ for (auto f : quickFindFiles(m_o.temp.c_str(), L"*"))
+ if (f.fileName == L"." || f.fileName == L"..")
+ continue;
+ else if ((f.attributes & FILE_ATTRIBUTE_DIRECTORY) == 0) {
+ if (f.fileName == m_o.output.filename())
+ output_file = true;
+ cleanfiles.push_back(f.fileName);
+ } else if (f.fileName == SOURCE_DIR || f.fileName == MOUNT_DIR)
+ cleandirs.push_back(f.fileName);
+ else
+ otherdirs.push_back(f.fileName);
+ if (!cleanfiles.empty() || !cleandirs.empty() || !otherdirs.empty()) {
+ if (!m_o.force_temp_cleanup && !otherdirs.empty())
+ throw FuncFailed("cleanup_temp",
+ "Refusing to clean temp dir with non-mount/source directories "
+ "(clean manually and rerun)",
+ m_o.temp.string().c_str());
+ if (!m_o.force_temp_cleanup && cleandirs.empty() && !output_file)
+ throw FuncFailed("cleanup_temp",
+ "Refusing to clean temp dir with no directories and no output "
+ "log (clean manually and rerun)",
+ m_o.temp.string().c_str());
+ std::wcout << "Cleaning previous temp dir: " << m_o.temp.c_str() << std::endl;
+ for (auto f : cleanfiles)
+ delete_file(m_o.temp / f);
+ for (auto d : cleandirs)
+ recursive_delete_files(m_o.temp / d);
+ for (auto d : otherdirs)
+ recursive_delete_files(m_o.temp / d);
+ }
+ }
+}
+
+void usvfs_test_base::copy_fixture()
+{
+ using namespace test;
+ using namespace winapi::ex::wide;
+
+ path fmount = m_o.fixture / MOUNT_DIR;
+ path fsource = m_o.fixture / SOURCE_DIR;
+
+ bool isDir = false;
+ if (!fileExists(fmount.c_str(), &isDir) || !isDir)
+ throw FuncFailed("copy_fixture", "fixtures dir does not exist",
+ fmount.string().c_str());
+ if (!fileExists(fsource.c_str(), &isDir) || !isDir)
+ throw FuncFailed("copy_fixture", "fixtures dir does not exist",
+ fsource.string().c_str());
+ if (fileExists(m_o.mount.c_str(), &isDir))
+ throw FuncFailed("copy_fixture", "source dir already exists",
+ m_o.mount.string().c_str());
+ if (fileExists(m_o.source.c_str(), &isDir))
+ throw FuncFailed("copy_fixture", "source dir already exists",
+ m_o.source.string().c_str());
+
+ std::wcout << "Copying fixture: " << m_o.fixture << std::endl;
+ recursive_copy_files(fmount, m_o.mount, false);
+ recursive_copy_files(fsource, m_o.source, false);
+}
+
+bool usvfs_test_base::postmortem_check()
+{
+ path gold_output = m_o.fixture / m_o.output.filename();
+
+ {
+ const auto& log = output();
+
+ path mount_gold = MOUNT_DIR;
+ mount_gold += POSTMORTEM_SUFFIX;
+ path source_gold = SOURCE_DIR;
+ source_gold += POSTMORTEM_SUFFIX;
+
+ bool is_dir = false;
+ if (!winapi::ex::wide::fileExists(m_o.mount.c_str(), &is_dir) || !is_dir) {
+ fprintf(log, " ERROR: mount directory does not exist?!\n");
+ return false;
+ }
+ if (!winapi::ex::wide::fileExists(m_o.source.c_str(), &is_dir) || !is_dir) {
+ fprintf(log, " ERROR: source directory does not exist?!\n");
+ return false;
+ }
+ if (!winapi::ex::wide::fileExists((m_o.fixture / mount_gold).c_str(), &is_dir) ||
+ !is_dir) {
+ fprintf(log, " ERROR: fixtures golden mount does not exist: %s\n",
+ mount_gold.string().c_str());
+ return false;
+ }
+ if (!winapi::ex::wide::fileExists((m_o.fixture / source_gold).c_str(), &is_dir) ||
+ !is_dir) {
+ fprintf(log, " ERROR: fixtures golden source does not exist: %s\n",
+ mount_gold.string().c_str());
+ return false;
+ }
+ if (!winapi::ex::wide::fileExists(gold_output.c_str(), &is_dir) || is_dir) {
+ fprintf(log, " ERROR: golden scenario output does not exist: %s\n",
+ gold_output.filename().string().c_str());
+ return false;
+ }
+
+ fprintf(log, "postmortem check of %s against golden %s...\n",
+ m_o.mount.filename().string().c_str(), mount_gold.string().c_str());
+ bool mount_check =
+ recursive_compare_dirs(path(), m_o.fixture / mount_gold, m_o.mount, log);
+
+ fprintf(log, "postmortem check of %s against golden %s...\n",
+ m_o.source.filename().string().c_str(), source_gold.string().c_str());
+ bool source_check =
+ recursive_compare_dirs(path(), m_o.fixture / source_gold, m_o.source, log);
+
+ if (mount_check && source_check)
+ fprintf(log, "postmortem check successful.\n");
+ else {
+ fprintf(log, "ERROR: postmortem check failed!\n");
+ return false;
+ }
+ } // close output before comparing it
+
+ // don't print anything more to the output (except maybe errors),
+ // so that the final output can be copied as is to the fixtures (when updating the
+ // golden version)
+
+ // block remove 2024/06/07 - one would need to regenerate a gold output for this but
+ // I am not sure this is still relevant
+ //
+ // if (!test::compare_files(gold_output, m_o.output, false)) {
+ // fprintf(output(), "ERROR: output does not match gold output: %s\n",
+ // m_o.output.filename().string().c_str()); return false;
+ //}
+
+ return true;
+}
+
+bool usvfs_test_base::recursive_compare_dirs(path rel_path, path gold_base,
+ path result_base, FILE* log)
+{
+ path result_full = result_base / rel_path;
+ path gold_full = gold_base / rel_path;
+
+ std::unordered_set<std::wstring> gold_dirs;
+ std::unordered_set<std::wstring> gold_files;
+ for (const auto& f : winapi::ex::wide::quickFindFiles(gold_full.c_str(), L"*")) {
+ if (f.fileName == L"." || f.fileName == L"..")
+ continue;
+ if (f.attributes & FILE_ATTRIBUTE_DIRECTORY)
+ gold_dirs.insert(f.fileName);
+ else
+ gold_files.insert(f.fileName);
+ }
+
+ bool all_good = true;
+
+ std::vector<std::wstring> recurse;
+ for (const auto& f : winapi::ex::wide::quickFindFiles(result_full.c_str(), L"*")) {
+ if (f.fileName == L"." || f.fileName == L"..")
+ continue;
+ if (f.attributes & FILE_ATTRIBUTE_DIRECTORY) {
+ const auto& find = gold_dirs.find(f.fileName);
+ if (find != gold_dirs.end()) {
+ gold_dirs.erase(find);
+ recurse.push_back(f.fileName);
+ } else {
+ fprintf(log, " unexpected directory found: %s%s\n", MOUNT_LABEL,
+ (rel_path / f.fileName).string().c_str());
+ all_good = false;
+ }
+ } else {
+ const auto& find = gold_files.find(f.fileName);
+ if (find != gold_files.end()) {
+ gold_files.erase(find);
+ if (!test::compare_files(gold_full / f.fileName, result_full / f.fileName,
+ false)) {
+ fprintf(log, " file contents differs: %s%s\n", MOUNT_LABEL,
+ (rel_path / f.fileName).string().c_str());
+ all_good = false;
+ }
+ } else {
+ fprintf(log, " unexpected file found: %s%s\n", MOUNT_LABEL,
+ (rel_path / f.fileName).string().c_str());
+ all_good = false;
+ }
+ }
+ }
+
+ for (auto d : gold_dirs) {
+ fprintf(log, " expected directory not found: %s%s\n", MOUNT_LABEL,
+ (rel_path / d).string().c_str());
+ all_good = false;
+ }
+
+ for (auto f : gold_files) {
+ fprintf(log, " expected file not found: %s%s\n", MOUNT_LABEL,
+ (rel_path / f).string().c_str());
+ all_good = false;
+ }
+
+ for (auto r : recurse)
+ all_good &= recursive_compare_dirs(rel_path / r, gold_base, result_base, log);
+
+ return all_good;
+}
+
+test::ScopedFILE usvfs_test_base::output()
+{
+ auto log = test::ScopedFILE::open(m_o.output, m_clean_output ? L"wt" : L"at");
+ m_clean_output = false;
+ return log;
+}
+
+void usvfs_test_base::clean_output()
+{
+ using namespace std;
+
+ errno_t err;
+ auto in = test::ScopedFILE::open(m_o.output, L"rt", err);
+ if (err == ENOENT) {
+ wcerr << L"warning: no " << m_o.output << L" to clean." << endl;
+ return;
+ } else if (err || !in)
+ throw_testWinFuncFailed("_wfopen_s", m_o.output.string().c_str(), err);
+
+ path clean = m_o.output.parent_path() / m_o.output.stem();
+ clean += OUTPUT_CLEAN_SUFFIX;
+ clean += m_o.output.extension();
+
+ auto out = test::ScopedFILE::open(clean, L"wt");
+ wcout << L"Cleaning " << m_o.output << " to " << clean << endl;
+
+ char line[1024];
+ while (!feof(in)) {
+ // read one line:
+ if (!fgets(line, _countof(line), in))
+ if (feof(in))
+ break;
+ else
+ throw_testWinFuncFailed("fgets", "reading output");
+ if (*line == '#')
+ continue;
+
+ // in order for the clean output to compare cleanly between run with different
+ // options we clean out things like the platform and the ops log name (which
+ // contians the scenario label):
+
+ char* platform = line;
+ while (platform) {
+ char* platform_x86 = strstr(platform, "x86");
+ char* platform_x64 = strstr(platform, "x64");
+ if (platform_x86 && platform_x64)
+ platform = std::min(platform_x86, platform_x64);
+ else if (platform_x86)
+ platform = platform_x86;
+ else if (platform_x64)
+ platform = platform_x64;
+ else
+ platform = nullptr;
+ if (platform) {
+ platform[1] = platform[2] = '?';
+ platform += 3;
+ }
+ }
+
+ char* cout_end = strstr(line, "-cout+ ");
+ char* cout_log_end = nullptr;
+ if (cout_end) {
+ cout_end += strlen("-cout+ ");
+ cout_log_end = strchr(cout_end, ' ');
+ }
+ if (cout_log_end && cout_log_end > cout_end) {
+ cout_end[0] = '?';
+ if (cout_log_end > cout_end + 1)
+ memmove(cout_end + 1, cout_log_end, strlen(cout_log_end) + 1);
+ }
+
+ fputs(line, out);
+ }
+}
+
+int usvfs_test_base::run(const std::wstring& exe_name)
+{
+ using namespace usvfs::shared;
+ using namespace std;
+
+ int res = run_impl(exe_name);
+ try {
+ clean_output();
+ } catch (const exception& e) {
+ wcerr << "CERROR: " << string_cast<wstring>(e.what(), CodePage::UTF8).c_str()
+ << endl;
+ } catch (...) {
+ wcerr << "CERROR: unknown exception" << endl;
+ }
+ if (!res)
+ wcout << "scenario " << scenario_name() << " PASSED." << endl;
+ else
+ wcerr << "scenario " << scenario_name() << " FAILED!" << endl;
+ return res;
+}
+
+int usvfs_test_base::run_impl(const std::wstring& exe_name)
+{
+ using namespace usvfs::shared;
+ using namespace std;
+
+ try {
+ winapi::ex::wide::createPath(m_o.output.parent_path().c_str());
+
+ // we read mappings first only because it is "non-destructive" but might raise an
+ // error if mappings invalid
+ auto mappings = mappings_reader(m_o.mount, m_o.source).read(m_o.mapping);
+
+ cleanup_temp();
+ log_settings(exe_name);
+ copy_fixture();
+
+ usvfs_connector usvfs(m_o);
+ {
+ const auto& log = output();
+ usvfs.updateMapping(mappings, m_o, log);
+
+ fprintf(log, "running scenario %s:\n\n", scenario_name());
+ }
+ auto res = scenario_run();
+ {
+ const auto& log = output();
+ if (res)
+ fprintf(log, "\nscenario ended successfully!\n\n");
+ else
+ fprintf(log, "\nscenario failed miserably.\n");
+ }
+
+ if (!res)
+ return 7;
+
+ if (!postmortem_check())
+ return 8;
+
+ return 0;
+ }
+#if 1 // just a convient way to not catch exception when debugging
+ catch (const exception& e) {
+ try {
+ wcerr << "ERROR: " << string_cast<wstring>(e.what(), CodePage::UTF8).c_str()
+ << endl;
+ fprintf(output(), "ERROR: %s\n", e.what());
+ } catch (const exception& e) {
+ wcerr << "ERROR^2: " << string_cast<wstring>(e.what(), CodePage::UTF8).c_str()
+ << endl;
+ } catch (...) {
+ wcerr << "ERROR^2: unknown exception" << endl;
+ }
+ } catch (...) {
+ try {
+ wcerr << "ERROR: unknown exception" << endl;
+ fprintf(output(), "ERROR: unknown exception\n");
+ } catch (const exception& e) {
+ wcerr << "ERROR^2: " << string_cast<wstring>(e.what(), CodePage::UTF8).c_str()
+ << endl;
+ } catch (...) {
+ wcerr << "ERROR^2: unknown exception" << endl;
+ }
+ }
+#else
+ catch (bool) {
+ }
+#endif
+ return 9; // exception
+}
+
+void usvfs_test_base::log_settings(const std::wstring& exe_name)
+{
+ using namespace usvfs::shared;
+ fprintf(output(), "%s %s started with %s%s%s\n\n",
+ string_cast<std::string>(exe_name).c_str(), scenario_name(),
+ m_o.opsexe.filename().string().c_str(), m_o.ops_options.empty() ? "" : " ",
+ string_cast<std::string>(m_o.ops_options).c_str());
+}
+
+void usvfs_test_base::ops_list(const path& rel_path, bool recursive, bool with_contents,
+ bool should_succeed, const wstring& additional_args)
+{
+ wstring cmd = recursive ? L"-r -list" : L"-list";
+ if (with_contents)
+ cmd += L"contents";
+ run_ops(should_succeed, cmd, rel_path, additional_args);
+}
+
+void usvfs_test_base::ops_read(const path& rel_path, bool should_succeed,
+ const wstring& additional_args)
+{
+ run_ops(should_succeed, L"-read", rel_path, additional_args);
+}
+
+void usvfs_test_base::ops_rewrite(const path& rel_path, const char* contents,
+ bool should_succeed, const wstring& additional_args)
+{
+ using namespace usvfs::shared;
+ run_ops(should_succeed, L"-rewrite", rel_path, additional_args,
+ L"\"" + string_cast<wstring>(contents, CodePage::UTF8) + L"\"");
+}
+
+void usvfs_test_base::ops_overwrite(const path& rel_path, const char* contents,
+ bool recursive, bool should_succeed,
+ const wstring& additional_args)
+{
+ using namespace usvfs::shared;
+ run_ops(should_succeed, recursive ? L"-r -overwrite" : L"-overwrite", rel_path,
+ additional_args,
+ L"\"" + string_cast<wstring>(contents, CodePage::UTF8) + L"\"");
+}
+
+void usvfs_test_base::ops_touch(const path& rel_path, bool full_write_access,
+ bool should_succeed, const wstring& additional_args)
+{
+ run_ops(should_succeed, full_write_access ? L"-touchw" : L"-touch", rel_path,
+ additional_args);
+}
+
+void usvfs_test_base::ops_deleteoverwrite(const path& rel_path, const char* contents,
+ bool recursive, bool should_succeed,
+ const wstring& additional_args)
+{
+ using namespace usvfs::shared;
+ run_ops(should_succeed, recursive ? L"-r -deleteoverwrite" : L"-deleteoverwrite",
+ rel_path, additional_args,
+ L"\"" + string_cast<wstring>(contents, CodePage::UTF8) + L"\"");
+}
+
+void usvfs_test_base::ops_delete(const path& rel_path, bool should_succeed,
+ const wstring& additional_args)
+{
+ run_ops(should_succeed, L"-delete", rel_path, additional_args);
+}
+
+void usvfs_test_base::ops_copy(const path& src_rel_path, const path& dest_rel_path,
+ bool replace, bool should_succeed,
+ const wstring& additional_args)
+{
+ run_ops(should_succeed, replace ? L"-copyover" : L"-copy", src_rel_path,
+ additional_args, wstring(), dest_rel_path);
+}
+
+void usvfs_test_base::ops_rename(const path& src_rel_path, const path& dest_rel_path,
+ bool replace, bool allow_copy, bool should_succeed,
+ const wstring& additional_args)
+{
+ wstring command = allow_copy ? L"-move" : L"-rename";
+ if (replace)
+ command += L"over";
+ run_ops(should_succeed, command, src_rel_path, additional_args, wstring(),
+ dest_rel_path);
+}
+
+void usvfs_test_base::ops_deleterename(const path& src_rel_path,
+ const path& dest_rel_path, bool allow_copy,
+ bool should_succeed,
+ const wstring& additional_args)
+{
+ wstring command = allow_copy ? L"-deletemove" : L"-deleterename";
+ run_ops(should_succeed, command, src_rel_path, additional_args, wstring(),
+ dest_rel_path);
+}
+
+void usvfs_test_base::run_ops(bool should_succeed, const wstring& preargs,
+ const path& rel_path, const wstring& additional_args,
+ const wstring& postargs, const path& rel_path2)
+{
+ using namespace usvfs::shared;
+ using string = std::string;
+ using wstring = wstring;
+
+ string commandlog = test::path(m_o.opsexe).filename().string();
+ wstring commandline = m_o.opsexe;
+ if (commandline.find(' ') != wstring::npos &&
+ commandline.find('"') == wstring::npos) {
+ commandline = L"\"" + commandline + L"\"";
+ commandlog = "\"" + commandlog + "\"";
+ }
+
+ if (!m_o.mount.empty()) {
+ commandline += L" -basedir ";
+ commandline += m_o.mount;
+ commandlog += " -basedir ";
+ commandlog += m_o.mount.filename().string();
+ }
+
+ if (!m_o.ops_options.empty()) {
+ commandline += L" ";
+ commandline += m_o.ops_options;
+ commandlog += " ";
+ commandlog += string_cast<string>(m_o.ops_options, CodePage::UTF8);
+ }
+
+ commandline += L" -cout+ ";
+ commandline += m_o.output;
+ commandlog += " -cout+ ";
+ commandlog += m_o.output.filename().string();
+
+ if (!additional_args.empty()) {
+ commandline += L" ";
+ commandline += additional_args;
+ commandlog += " ";
+ commandlog += string_cast<string>(additional_args, CodePage::UTF8);
+ }
+
+ if (!preargs.empty()) {
+ commandline += L" ";
+ commandline += preargs;
+ commandlog += " ";
+ commandlog += string_cast<string>(preargs, CodePage::UTF8);
+ }
+
+ if (!rel_path.empty()) {
+ commandline += L" ";
+ commandline += m_o.mount / rel_path;
+ commandlog += " ";
+ commandlog += MOUNT_LABEL + rel_path.string();
+ }
+
+ if (!rel_path2.empty()) {
+ commandline += L" ";
+ commandline += m_o.mount / rel_path2;
+ commandlog += " ";
+ commandlog += MOUNT_LABEL + rel_path2.string();
+ }
+
+ if (!postargs.empty()) {
+ commandline += L" ";
+ commandline += postargs;
+ commandlog += " ";
+ commandlog += string_cast<string>(postargs, CodePage::UTF8);
+ }
+
+ fprintf(output(), "Spawning: %s\n", commandlog.c_str());
+ auto res = usvfs_connector::spawn(&commandline[0]);
+ fprintf(output(), "\n");
+
+ bool success = res == 0;
+ if (success != should_succeed)
+ throw test::FuncFailed("run_ops", success ? "succeeded" : "failed",
+ commandlog.c_str(), res);
+}
+
+std::string usvfs_test_base::mount_contents(const path& rel_path)
+{
+ verify_mount_existance(rel_path);
+ const auto& contents = test::read_small_file(m_o.mount / rel_path);
+ return std::string(contents.data(), contents.size());
+}
+
+std::string usvfs_test_base::source_contents(const path& rel_path)
+{
+ verify_source_existance(rel_path);
+ const auto& contents = test::read_small_file(m_o.source / rel_path);
+ return std::string(contents.data(), contents.size());
+}
+
+void usvfs_test_base::verify_mount_contents(const path& rel_path, const char* contents)
+{
+ verify_mount_existance(rel_path);
+ if (verify_contents(m_o.mount / rel_path, contents))
+ throw test::FuncFailed("verify_mount_contents",
+ (MOUNT_LABEL + rel_path.string()).c_str(), contents);
+}
+
+void usvfs_test_base::verify_source_contents(const path& rel_path, const char* contents)
+{
+ verify_source_existance(rel_path);
+ if (verify_contents(m_o.source / rel_path, contents))
+ throw test::FuncFailed("verify_source_contents",
+ (SOURCE_LABEL + rel_path.string()).c_str(), contents);
+}
+
+bool usvfs_test_base::verify_contents(const path& file, const char* contents)
+{
+ // we allow difference in trailing whitespace (i.e. extra new line):
+
+ size_t sz = strlen(contents);
+ while (sz && isspace(contents[sz - 1]))
+ --sz;
+
+ const auto& real_contents = test::read_small_file(file);
+ size_t real_sz = real_contents.size();
+ while (real_sz && isspace(real_contents[real_sz - 1]))
+ --real_sz;
+
+ return sz == real_sz && memcmp(contents, real_contents.data(), sz);
+}
+
+void usvfs_test_base::verify_mount_existance(const path& rel_path, bool exists,
+ bool is_dir)
+{
+ bool real_is_dir = false;
+ bool real_exists =
+ winapi::ex::wide::fileExists((m_o.mount / rel_path).c_str(), &real_is_dir);
+ if (exists != real_exists)
+ throw test::FuncFailed("verify_mount_existance",
+ real_exists ? "path exists" : "path does not exist",
+ (MOUNT_LABEL + rel_path.string()).c_str());
+ else if (real_exists && is_dir != real_is_dir)
+ throw test::FuncFailed("verify_mount_existance",
+ real_is_dir ? "path is a directory" : "path is a file",
+ (MOUNT_LABEL + rel_path.string()).c_str());
+}
+
+void usvfs_test_base::verify_source_existance(const path& rel_path, bool exists,
+ bool is_dir)
+{
+ bool real_is_dir = false;
+ bool real_exists =
+ winapi::ex::wide::fileExists((m_o.source / rel_path).c_str(), &real_is_dir);
+ if (exists != real_exists)
+ throw test::FuncFailed("verify_source_existance",
+ real_exists ? "path exists" : "path does not exist",
+ (SOURCE_LABEL + rel_path.string()).c_str());
+ else if (real_exists && is_dir != real_is_dir)
+ throw test::FuncFailed("verify_source_existance",
+ real_is_dir ? "path is a directory" : "path is a file",
+ (SOURCE_LABEL + rel_path.string()).c_str());
+}
diff --git a/libs/usvfs/test/usvfs_test_runner/usvfs_test/usvfs_test_base.h b/libs/usvfs/test/usvfs_test_runner/usvfs_test/usvfs_test_base.h
new file mode 100644
index 0000000..4986e3b
--- /dev/null
+++ b/libs/usvfs/test/usvfs_test_runner/usvfs_test/usvfs_test_base.h
@@ -0,0 +1,120 @@
+#pragma once
+
+#include <string>
+
+#include <test_helpers.h>
+
+class usvfs_test_options
+{
+public:
+ static constexpr auto DEFAULT_MAPPING = L"vfs_mappings.txt";
+ static constexpr auto MOUNT_DIR = L"mount";
+ static constexpr auto SOURCE_DIR = L"source";
+
+ using path = test::path;
+
+ // fills any values not set (or set to an empty value) to their default value
+ void fill_defaults(const path& test_name, const std::wstring& scenario,
+ const wchar_t* label);
+
+ void set_ops32(); // sets opsexe iff opsexe is empty
+ void set_ops64(); // sets opsexe iff opsexe is empty
+ void add_ops_options(const std::wstring& options);
+
+ path opsexe;
+ path fixture;
+ path mapping;
+ path temp;
+ path mount;
+ path source;
+ path output;
+ path usvfs_log;
+ std::wstring ops_options;
+ bool temp_cleanup = false;
+ bool force_temp_cleanup = false;
+};
+
+class usvfs_test_base
+{
+public:
+ static constexpr auto MOUNT_DIR = usvfs_test_options::MOUNT_DIR;
+ static constexpr auto SOURCE_DIR = usvfs_test_options::SOURCE_DIR;
+ static constexpr auto MOUNT_LABEL = "mount:";
+ static constexpr auto SOURCE_LABEL = "source:";
+ static constexpr auto OUTPUT_CLEAN_SUFFIX = L"_clean";
+ static constexpr auto POSTMORTEM_SUFFIX = L".postmortem";
+
+ using wstring = std::wstring;
+ using path = test::path;
+
+ // options object should outlive this object.
+ usvfs_test_base(const usvfs_test_options& options) : m_o(options) {}
+ virtual ~usvfs_test_base() = default;
+
+ int run(const std::wstring& exe_name);
+
+ // function for override:
+
+ virtual const char* scenario_name() = 0;
+ virtual bool scenario_run() = 0;
+
+ // helpers for derived scenarios:
+
+ virtual void ops_list(const path& rel_path, bool recursive, bool with_contents,
+ bool should_succeed = true,
+ const wstring& additional_args = wstring());
+ virtual void ops_read(const path& rel_path, bool should_succeed = true,
+ const wstring& additional_args = wstring());
+ virtual void ops_rewrite(const path& rel_path, const char* contents,
+ bool should_succeed = true,
+ const wstring& additional_args = wstring());
+ virtual void ops_overwrite(const path& rel_path, const char* contents, bool recursive,
+ bool should_succeed = true,
+ const wstring& additional_args = wstring());
+ virtual void ops_touch(const path& rel_path, bool full_write_access = false,
+ bool should_succeed = true,
+ const wstring& additional_args = wstring());
+ virtual void ops_deleteoverwrite(const path& rel_path, const char* contents,
+ bool recursive, bool should_succeed = true,
+ const wstring& additional_args = wstring());
+ virtual void ops_delete(const path& rel_path, bool should_succeed = true,
+ const wstring& additional_args = wstring());
+ virtual void ops_copy(const path& src_rel_path, const path& dest_rel_path,
+ bool replace, bool should_succeed = true,
+ const wstring& additional_args = wstring());
+ virtual void ops_rename(const path& src_rel_path, const path& dest_rel_path,
+ bool replace, bool allow_copy = false,
+ bool should_succeed = true,
+ const wstring& additional_args = wstring());
+ virtual void ops_deleterename(const path& src_rel_path, const path& dest_rel_path,
+ bool allow_copy = false, bool should_succeed = true,
+ const wstring& additional_args = wstring());
+
+ virtual std::string mount_contents(const path& rel_path);
+ virtual void verify_mount_contents(const path& rel_path, const char* contents);
+ virtual void verify_mount_existance(const path& rel_path, bool exists = true,
+ bool is_dir = false);
+ virtual std::string source_contents(const path& rel_path);
+ virtual void verify_source_contents(const path& rel_path, const char* contents);
+ virtual void verify_source_existance(const path& rel_path, bool exists = true,
+ bool is_dir = false);
+
+private:
+ int run_impl(const std::wstring& exe_name);
+ void log_settings(const std::wstring& exe_name);
+ void cleanup_temp();
+ void copy_fixture();
+ bool postmortem_check();
+ bool recursive_compare_dirs(path rel_path, path gold_base, path result_base,
+ FILE* log);
+ void clean_output();
+
+ test::ScopedFILE output();
+ void run_ops(bool should_succeed, const wstring& preargs, const path& rel_path,
+ const wstring& additional_args, const wstring& postargs = wstring(),
+ const path& rel_path2 = path());
+ bool verify_contents(const path& file, const char* contents);
+
+ const usvfs_test_options& m_o;
+ bool m_clean_output = true;
+};
diff --git a/libs/usvfs/test/usvfs_test_runner/usvfs_test_runner.cpp b/libs/usvfs/test/usvfs_test_runner/usvfs_test_runner.cpp
new file mode 100644
index 0000000..a70fb0f
--- /dev/null
+++ b/libs/usvfs/test/usvfs_test_runner/usvfs_test_runner.cpp
@@ -0,0 +1,105 @@
+#include <gtest/gtest.h>
+
+#include <boost/filesystem.hpp>
+#include <iostream>
+#include <test_helpers.h>
+#include <windows_sane.h>
+
+static std::string usvfs_test_command(const char* scenario, const char* platform,
+ const char* testflag = nullptr,
+ const char* opsarg = nullptr)
+{
+ using namespace test;
+ std::string command =
+ path_of_test_bin(platform_dependant_executable("usvfs_test", "exe", platform))
+ .string();
+ if (testflag) {
+ command += " -";
+ command += testflag;
+ }
+ if (opsarg) {
+ command += " -opsarg -";
+ command += opsarg;
+ }
+ command += " ";
+ command += scenario;
+ if (testflag || opsarg) {
+ command += ":";
+ if (testflag) {
+ command += testflag;
+ command += "_";
+ }
+ if (opsarg) {
+ command += opsarg;
+ command += "_";
+ }
+ command += platform;
+ }
+ return command;
+}
+
+static DWORD spawn(std::string commandline)
+{
+ STARTUPINFOA si{0};
+ si.cb = sizeof(si);
+ PROCESS_INFORMATION pi{0};
+
+ std::cout << "Running: [" << commandline << "]" << std::endl;
+ if (!CreateProcessA(NULL, commandline.data(), NULL, NULL, FALSE, 0, NULL, NULL, &si,
+ &pi)) {
+ DWORD gle = GetLastError();
+ std::cerr << "CreateProcess failed error=" << gle << std::endl;
+ return 98;
+ }
+
+ WaitForSingleObject(pi.hProcess, INFINITE);
+
+ DWORD exit = 99;
+ if (!GetExitCodeProcess(pi.hProcess, &exit)) {
+ DWORD gle = GetLastError();
+ std::cerr << "GetExitCodeProcess failed error=" << gle << std::endl;
+ }
+
+ CloseHandle(pi.hProcess);
+ CloseHandle(pi.hThread);
+
+ return exit;
+}
+
+TEST(UsvfsTest, basic_x64)
+{
+ EXPECT_EQ(0, spawn(usvfs_test_command("basic", "x64")));
+}
+
+TEST(UsvfsTest, basic_x86)
+{
+ EXPECT_EQ(0, spawn(usvfs_test_command("basic", "x86")));
+}
+
+TEST(UsvfsTest, basic_ops32_x64)
+{
+ EXPECT_EQ(0, spawn(usvfs_test_command("basic", "x64", "ops32")));
+}
+
+TEST(UsvfsTest, basic_ops64_x86)
+{
+ EXPECT_EQ(0, spawn(usvfs_test_command("basic", "x86", "ops64")));
+}
+
+/*
+TEST(UsvfsTest, basic_ntapi_x64)
+{
+ EXPECT_EQ(0, spawn(usvfs_test_command("basic", "x64", nullptr, "ntapi")));
+}
+
+TEST(UsvfsTest, basic_ntapi_x86)
+{
+ EXPECT_EQ(0, spawn(usvfs_test_command("basic", "x86", nullptr, "ntapi")));
+}
+*/
+
+int main(int argc, char** argv)
+{
+ testing::InitGoogleTest(&argc, argv);
+ return RUN_ALL_TESTS();
+}