upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/test/process_cmdline_test.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/test/process_cmdline_test.cpp')
-rw-r--r--runtime/test/process_cmdline_test.cpp56
1 files changed, 56 insertions, 0 deletions
diff --git a/runtime/test/process_cmdline_test.cpp b/runtime/test/process_cmdline_test.cpp
new file mode 100644
index 000000000..0df7b4e86
--- /dev/null
+++ b/runtime/test/process_cmdline_test.cpp
@@ -0,0 +1,56 @@
1// The process shim's command line round-trips: what GetCommandLineW says must parse with CommandLineToArgvW (the
2// documented rules the CRT's argv parser shares) back to the __wargv the shim hands out, and __wargv must hold what
3// main.cpp passed. No game files.
4#include <windows.h>
5#include <shellapi.h>
6
7#include <cstdio>
8#include <filesystem>
9#include <string>
10#include <vector>
11
12#include "process_shim.h"
13#include "shims.h"
14
15namespace {
16int failures = 0;
17
18wchar_t** wargv() {
19 using Fn = wchar_t*** (__cdecl*)();
20 return *reinterpret_cast<Fn>(rt::shims::find("ucrtbase.dll", "__p___wargv"))();
21}
22
23void round_trip(const std::vector<std::string>& args, const char* what) {
24 rt::process::set_command_line("game.exe", args);
25 using Fn = LPWSTR(WINAPI*)();
26 const LPWSTR line = reinterpret_cast<Fn>(rt::shims::find("kernel32.dll", "GetCommandLineW"))();
27 int count = 0;
28 LPWSTR* parsed = CommandLineToArgvW(line, &count);
29 bool ok = parsed && count == int(args.size()) + 1;
30 for (int i = 0; ok && i < count; ++i) ok = wargv()[i] && wcscmp(parsed[i], wargv()[i]) == 0;
31 if (!ok) {
32 std::fwprintf(stderr, L"FAIL %hs: GetCommandLineW [%ls] parses to %d argument(s)\n", what, line, count);
33 ++failures;
34 }
35 LocalFree(parsed);
36}
37} // namespace
38
39int main() {
40 round_trip({"plain", "with space"}, "plain and spaced arguments");
41 round_trip({"C:\\My Games\\", "next"}, "a quoted argument ending in a backslash");
42 round_trip({"a\\\"b"}, "a backslash before a quote");
43 round_trip({"\\\\server\\share\\x y"}, "a UNC path with a space");
44
45 // main.cpp passes game arguments as std::filesystem::path::string(): ANSI code page text, not UTF-8.
46 const std::wstring cafe = L"caf\u00e9";
47 round_trip({std::filesystem::path(cafe).string()}, "a non-ASCII argument");
48 if (!wargv()[1] || cafe != wargv()[1]) {
49 std::fprintf(stderr, "FAIL: __wargv[1] is not L\"caf\\u00e9\"\n");
50 ++failures;
51 }
52
53 if (failures) return 1;
54 std::printf("process_cmdline: GetCommandLineW parses back to __wargv, non-ASCII arguments intact\n");
55 return 0;
56}