aboutsummaryrefslogtreecommitdiff
path: root/libs/usvfs/test/tinjectlib_test
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/tinjectlib_test
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/tinjectlib_test')
-rw-r--r--libs/usvfs/test/tinjectlib_test/CMakeLists.txt16
-rw-r--r--libs/usvfs/test/tinjectlib_test/main.cpp158
-rw-r--r--libs/usvfs/test/tinjectlib_test/testinject_bin/main.cpp4
-rw-r--r--libs/usvfs/test/tinjectlib_test/testinject_dll/main.cpp36
-rw-r--r--libs/usvfs/test/tinjectlib_test/testinject_dll/main.h11
5 files changed, 225 insertions, 0 deletions
diff --git a/libs/usvfs/test/tinjectlib_test/CMakeLists.txt b/libs/usvfs/test/tinjectlib_test/CMakeLists.txt
new file mode 100644
index 0000000..9bd3f7b
--- /dev/null
+++ b/libs/usvfs/test/tinjectlib_test/CMakeLists.txt
@@ -0,0 +1,16 @@
+cmake_minimum_required(VERSION 3.16)
+
+find_package(GTest CONFIG REQUIRED)
+
+# binary and binary injected
+add_executable(testinject_bin testinject_bin/main.cpp)
+usvfs_set_test_properties(testinject_bin FOLDER tinjectlib)
+
+add_library(testinject_dll SHARED testinject_dll/main.cpp testinject_dll/main.h)
+usvfs_set_test_properties(testinject_dll FOLDER tinjectlib)
+
+# actual test executable
+add_executable(tinjectlib_test main.cpp)
+usvfs_set_test_properties(tinjectlib_test FOLDER tinjectlib)
+target_link_libraries(tinjectlib_test PRIVATE tinjectlib shared GTest::gtest GTest::gtest_main)
+add_dependencies(tinjectlib_test testinject_bin testinject_dll)
diff --git a/libs/usvfs/test/tinjectlib_test/main.cpp b/libs/usvfs/test/tinjectlib_test/main.cpp
new file mode 100644
index 0000000..ce48f83
--- /dev/null
+++ b/libs/usvfs/test/tinjectlib_test/main.cpp
@@ -0,0 +1,158 @@
+#include <boost/filesystem.hpp>
+#include <boost/type_traits.hpp>
+#include <gtest/gtest.h>
+#include <injectlib.h>
+#include <spdlog/sinks/stdout_sinks.h>
+#include <spdlog/spdlog.h>
+#include <winapi.h>
+
+using namespace usvfs::shared;
+using namespace InjectLib;
+
+#if BOOST_ARCH_X86_64
+static const wchar_t INJECT_BIN[] = L"testinject_bin_x64.exe";
+#else
+static const wchar_t INJECT_BIN[] = L"testinject_bin_x86.exe";
+#endif
+
+#if BOOST_ARCH_X86_64
+static const wchar_t INJECT_LIB[] = L"testinject_dll_x64.dll";
+#else
+static const wchar_t INJECT_LIB[] = L"testinject_dll_x86.dll";
+#endif
+
+static std::shared_ptr<spdlog::logger> logger()
+{
+ std::shared_ptr<spdlog::logger> result = spdlog::get("test");
+ if (result.get() == nullptr) {
+ result = spdlog::stdout_logger_mt("test");
+ }
+ return result;
+}
+
+bool spawn(HANDLE& processHandle, HANDLE& threadHandle)
+{
+ STARTUPINFO si;
+ ::ZeroMemory(&si, sizeof(si));
+ si.cb = sizeof(si);
+
+ PROCESS_INFORMATION pi;
+ BOOL success = ::CreateProcess(INJECT_BIN, nullptr, nullptr, nullptr, FALSE,
+ CREATE_SUSPENDED, nullptr, nullptr, &si, &pi);
+
+ if (!success) {
+ throw windows_error("failed to start process");
+ }
+
+ processHandle = pi.hProcess;
+ threadHandle = pi.hThread;
+
+ return true;
+}
+
+TEST(InjectingTest, InjectionNoInit)
+{
+ // Verify lib can inject without a init function
+
+ HANDLE process, thread;
+ spawn(process, thread);
+ EXPECT_NO_THROW(InjectLib::InjectDLL(process, thread, INJECT_LIB));
+ ResumeThread(thread);
+
+ DWORD res = WaitForSingleObject(process, INFINITE);
+ DWORD exitCode = NO_ERROR;
+ res = GetExitCodeProcess(process, &exitCode);
+ EXPECT_EQ(NOERROR, exitCode);
+
+ CloseHandle(process);
+ CloseHandle(thread);
+}
+
+TEST(InjectingTest, InjectionSimpleInit)
+{
+ // Verify lib can inject with a init function with null parameters
+
+ HANDLE process, thread;
+ spawn(process, thread);
+ EXPECT_NO_THROW(InjectLib::InjectDLL(process, thread, INJECT_LIB, "InitNoParam"));
+ ResumeThread(thread);
+
+ DWORD res = WaitForSingleObject(process, INFINITE);
+ DWORD exitCode = NO_ERROR;
+ res = GetExitCodeProcess(process, &exitCode);
+ EXPECT_EQ(10001, exitCode); // used init function exits process with this exit code
+
+ CloseHandle(process);
+ CloseHandle(thread);
+}
+
+TEST(InjectingTest, InjectionComplexInit)
+{
+ // Verify lib can inject with a init function with null parameters
+
+ static const WCHAR param[] = L"magic_parameter";
+ HANDLE process, thread;
+ spawn(process, thread);
+ EXPECT_NO_THROW(InjectLib::InjectDLL(process, thread, INJECT_LIB, "InitComplexParam",
+ reinterpret_cast<LPCVOID>(param),
+ wcslen(param) * sizeof(WCHAR)));
+
+ ResumeThread(thread);
+
+ DWORD res = WaitForSingleObject(process, INFINITE);
+ DWORD exitCode = NO_ERROR;
+ res = GetExitCodeProcess(process, &exitCode);
+ EXPECT_EQ(10002, exitCode); // used init function exits process with this exit code
+
+ CloseHandle(process);
+ CloseHandle(thread);
+}
+
+TEST(InjectingTest, InjectionNoQuitInit)
+{
+ // Verify lib can inject with a init function with null parameters
+
+ HANDLE process, thread;
+ spawn(process, thread);
+ EXPECT_NO_THROW(InjectLib::InjectDLL(process, thread, INJECT_LIB, "InitNoQuit"));
+ ResumeThread(thread);
+
+ DWORD res = WaitForSingleObject(process, INFINITE);
+ DWORD exitCode = NO_ERROR;
+ res = GetExitCodeProcess(process, &exitCode);
+ EXPECT_EQ(0, exitCode); // expect regular exit from process
+
+ CloseHandle(process);
+ CloseHandle(thread);
+}
+
+TEST(InjectingTest, InjectionSkipInit)
+{
+ // verify the skip-on-missing mechanism for init function works
+
+ HANDLE process, thread;
+ spawn(process, thread);
+ EXPECT_NO_THROW(InjectLib::InjectDLL(process, thread, INJECT_LIB, "__InitInvalid",
+ nullptr, 0, true));
+ ResumeThread(thread);
+
+ DWORD res = WaitForSingleObject(process, INFINITE);
+ DWORD exitCode = NO_ERROR;
+ res = GetExitCodeProcess(process, &exitCode);
+ EXPECT_EQ(NOERROR, exitCode);
+
+ CloseHandle(process);
+ CloseHandle(thread);
+}
+
+int main(int argc, char** argv)
+{
+ auto logger = spdlog::stdout_logger_mt("usvfs");
+ logger->set_level(spdlog::level::warn);
+
+ boost::filesystem::path filePath(winapi::wide::getModuleFileName(nullptr));
+ SetCurrentDirectoryW(filePath.parent_path().wstring().c_str());
+
+ testing::InitGoogleTest(&argc, argv);
+ return RUN_ALL_TESTS();
+}
diff --git a/libs/usvfs/test/tinjectlib_test/testinject_bin/main.cpp b/libs/usvfs/test/tinjectlib_test/testinject_bin/main.cpp
new file mode 100644
index 0000000..f8b643a
--- /dev/null
+++ b/libs/usvfs/test/tinjectlib_test/testinject_bin/main.cpp
@@ -0,0 +1,4 @@
+int main()
+{
+ return 0;
+}
diff --git a/libs/usvfs/test/tinjectlib_test/testinject_dll/main.cpp b/libs/usvfs/test/tinjectlib_test/testinject_dll/main.cpp
new file mode 100644
index 0000000..8339e97
--- /dev/null
+++ b/libs/usvfs/test/tinjectlib_test/testinject_dll/main.cpp
@@ -0,0 +1,36 @@
+#include "main.h"
+
+void __cdecl InitNoQuit(LPVOID, size_t)
+{
+ // nop
+}
+
+void __cdecl InitNoParam(LPVOID, size_t)
+{
+ ExitProcess(10001);
+}
+
+void __cdecl InitComplexParam(LPVOID userData, size_t)
+{
+ LPCWSTR string = (LPCWSTR)userData;
+ if (wcscmp(string, L"magic_parameter") == 0) {
+ ExitProcess(10002);
+ } else {
+ ExitProcess(20003);
+ }
+}
+
+BOOL APIENTRY DllMain(HMODULE, DWORD reasonForCall, LPVOID)
+{
+ switch (reasonForCall) {
+ case DLL_PROCESS_ATTACH: {
+ } break;
+ case DLL_PROCESS_DETACH: {
+ } break;
+ case DLL_THREAD_ATTACH: {
+ } break;
+ case DLL_THREAD_DETACH: {
+ } break;
+ }
+ return TRUE;
+}
diff --git a/libs/usvfs/test/tinjectlib_test/testinject_dll/main.h b/libs/usvfs/test/tinjectlib_test/testinject_dll/main.h
new file mode 100644
index 0000000..6faf800
--- /dev/null
+++ b/libs/usvfs/test/tinjectlib_test/testinject_dll/main.h
@@ -0,0 +1,11 @@
+#pragma once
+
+#define WIN32_LEAN_AND_MEAN
+#include <windows.h>
+
+extern "C"
+ __declspec(dllexport) void __cdecl InitNoQuit(LPVOID userData, size_t userDataSize);
+extern "C" __declspec(dllexport) void __cdecl InitNoParam(LPVOID userData,
+ size_t userDataSize);
+extern "C" __declspec(dllexport) void __cdecl InitComplexParam(LPVOID userData,
+ size_t userDataSize);