diff options
Diffstat (limited to 'runtime/test/gameinput_host_test.cpp')
| -rw-r--r-- | runtime/test/gameinput_host_test.cpp | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/runtime/test/gameinput_host_test.cpp b/runtime/test/gameinput_host_test.cpp new file mode 100644 index 000000000..cdd071055 --- /dev/null +++ b/runtime/test/gameinput_host_test.cpp | |||
| @@ -0,0 +1,90 @@ | |||
| 1 | // The host GameInput path: mapped code that imports GameInputCreate from GameInput.dll, with no GameInput shim. | ||
| 2 | // | ||
| 3 | // docs/gameinput.md: without the SDL3 shim, the loader binds GameInputCreate to the host's GameInput.dll. This checks | ||
| 4 | // that path with a synthetic DLL (test/fixtures/gameinput_host.cpp) mapped by rt::Loader: | ||
| 5 | // - its GameInput.dll import binds to the host's copy (this test links no shim table, so it cannot be a shim); | ||
| 6 | // - GameInputCreate succeeds from mapped code, and the library answers GetCurrentTimestamp; | ||
| 7 | // - GetCurrentReading says either "reading" or "no reading found": no crash, no other error. With a gamepad attached | ||
| 8 | // GetGamepadState must succeed on the reading; the test passes with or without one. | ||
| 9 | // Skips (exit 77) only where Windows itself cannot load GameInput.dll (asked before mapping, with LoadLibraryExW). | ||
| 10 | // When the DLL is there, an import that binds to nothing is a failure, not a skip. No game files. | ||
| 11 | #include <windows.h> | ||
| 12 | |||
| 13 | #include <cstdio> | ||
| 14 | #include <filesystem> | ||
| 15 | #include <stdexcept> | ||
| 16 | #include <string> | ||
| 17 | |||
| 18 | #include "pe_loader.h" | ||
| 19 | |||
| 20 | namespace { | ||
| 21 | |||
| 22 | using ProbeFn = int (*)(long*, unsigned long long*, long*, int*); | ||
| 23 | constexpr unsigned long kReadingNotFound = 0x838A0003UL; // GAMEINPUT_E_READING_NOT_FOUND | ||
| 24 | |||
| 25 | int g_failures = 0; | ||
| 26 | |||
| 27 | void check(bool ok, const std::string& what) { | ||
| 28 | std::printf("%s %s\n", ok ? "PASS" : "FAIL", what.c_str()); | ||
| 29 | std::fflush(stdout); | ||
| 30 | if (!ok) ++g_failures; | ||
| 31 | } | ||
| 32 | |||
| 33 | } // namespace | ||
| 34 | |||
| 35 | int main(int argc, char** argv) { | ||
| 36 | if (argc != 2) { | ||
| 37 | std::fprintf(stderr, "usage: gameinput_host_test <gameinput_host.dll>\n"); | ||
| 38 | return 2; | ||
| 39 | } | ||
| 40 | const std::filesystem::path fixture = argv[1]; | ||
| 41 | |||
| 42 | // Decide the skip from the machine, not from the loader's answer: a broken host binding must fail the test. | ||
| 43 | HMODULE const host_gameinput = LoadLibraryExW(L"GameInput.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32); | ||
| 44 | if (!host_gameinput) { | ||
| 45 | const DWORD error = GetLastError(); | ||
| 46 | if (error == ERROR_MOD_NOT_FOUND || error == ERROR_FILE_NOT_FOUND || error == ERROR_PATH_NOT_FOUND) { | ||
| 47 | std::printf("SKIP: no GameInput.dll on this machine\n"); | ||
| 48 | return 77; | ||
| 49 | } | ||
| 50 | std::printf("FAIL GameInput.dll is present but does not load (error %lu)\n", static_cast<unsigned long>(error)); | ||
| 51 | return 1; | ||
| 52 | } | ||
| 53 | |||
| 54 | try { | ||
| 55 | rt::Loader loader(fixture.parent_path()); | ||
| 56 | rt::Module& m = loader.load(fixture); | ||
| 57 | |||
| 58 | // The SDK's import library imports by ordinal, so match the DLL name, not the function name. | ||
| 59 | rt::Binding binding = rt::Binding::Unimplemented; | ||
| 60 | int seen = 0; | ||
| 61 | for (const auto& r : m.imports) | ||
| 62 | if (rt::lower(r.dll) == "gameinput.dll") { | ||
| 63 | binding = r.binding; | ||
| 64 | ++seen; | ||
| 65 | } | ||
| 66 | check(seen == 1 && binding == rt::Binding::Host, "the GameInput.dll import binds to the host's copy"); | ||
| 67 | |||
| 68 | loader.initialise(); | ||
| 69 | const auto probe = reinterpret_cast<ProbeFn>(m.exports.at("gameinput_host_probe")); | ||
| 70 | long create_hr = -1, reading_hr = -1; | ||
| 71 | unsigned long long timestamp = 0; | ||
| 72 | int state_ok = 0; | ||
| 73 | const int created = probe(&create_hr, ×tamp, &reading_hr, &state_ok); | ||
| 74 | std::printf(" create hr=0x%08lX, timestamp=%llu, reading hr=0x%08lX\n", static_cast<unsigned long>(create_hr), | ||
| 75 | timestamp, static_cast<unsigned long>(reading_hr)); | ||
| 76 | check(created == 1 && create_hr == 0, "GameInputCreate succeeds from mapped code"); | ||
| 77 | check(timestamp != 0, "GetCurrentTimestamp returns a time"); | ||
| 78 | const bool pad = created == 1 && reading_hr == 0; | ||
| 79 | check(pad || static_cast<unsigned long>(reading_hr) == kReadingNotFound, | ||
| 80 | "GetCurrentReading says either 'reading' or 'no reading found'"); | ||
| 81 | std::printf(" gamepad reading: %s\n", pad ? "yes (a gamepad is attached)" : "none (no gamepad attached)"); | ||
| 82 | if (pad) check(state_ok == 1, "GetGamepadState succeeds on a real reading"); | ||
| 83 | } catch (const std::exception& e) { | ||
| 84 | std::printf("FAIL unexpected exception: %s\n", e.what()); | ||
| 85 | ++g_failures; | ||
| 86 | } | ||
| 87 | FreeLibrary(host_gameinput); | ||
| 88 | std::printf("gameinput_host_test: %s (%d failures)\n", g_failures ? "FAILED" : "OK", g_failures); | ||
| 89 | return g_failures ? 1 : 0; | ||
| 90 | } | ||