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.cpp52
1 files changed, 35 insertions, 17 deletions
diff --git a/runtime/test/process_cmdline_test.cpp b/runtime/test/process_cmdline_test.cpp
index 0df7b4e86..4c78835bb 100644
--- a/runtime/test/process_cmdline_test.cpp
+++ b/runtime/test/process_cmdline_test.cpp
@@ -1,11 +1,10 @@
1// The process shim's command line round-trips: what GetCommandLineW says must parse with CommandLineToArgvW (the 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 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. 3// main.cpp passed, whatever the system code page (RT-84). No game files.
4#include <windows.h> 4#include <windows.h>
5#include <shellapi.h> 5#include <shellapi.h>
6 6
7#include <cstdio> 7#include <cstdio>
8#include <filesystem>
9#include <string> 8#include <string>
10#include <vector> 9#include <vector>
11 10
@@ -20,8 +19,13 @@ wchar_t** wargv() {
20 return *reinterpret_cast<Fn>(rt::shims::find("ucrtbase.dll", "__p___wargv"))(); 19 return *reinterpret_cast<Fn>(rt::shims::find("ucrtbase.dll", "__p___wargv"))();
21} 20}
22 21
23void round_trip(const std::vector<std::string>& args, const char* what) { 22char** argv() {
24 rt::process::set_command_line("game.exe", args); 23 using Fn = char*** (__cdecl*)();
24 return *reinterpret_cast<Fn>(rt::shims::find("ucrtbase.dll", "__p___argv"))();
25}
26
27void round_trip(const std::vector<std::wstring>& args, const char* what) {
28 rt::process::set_command_line(L"game.exe", args);
25 using Fn = LPWSTR(WINAPI*)(); 29 using Fn = LPWSTR(WINAPI*)();
26 const LPWSTR line = reinterpret_cast<Fn>(rt::shims::find("kernel32.dll", "GetCommandLineW"))(); 30 const LPWSTR line = reinterpret_cast<Fn>(rt::shims::find("kernel32.dll", "GetCommandLineW"))();
27 int count = 0; 31 int count = 0;
@@ -34,23 +38,37 @@ void round_trip(const std::vector<std::string>& args, const char* what) {
34 } 38 }
35 LocalFree(parsed); 39 LocalFree(parsed);
36} 40}
37} // namespace
38 41
39int main() { 42// __wargv[1] must be `want` exactly, and the narrow argv[1] its ANSI code page text (the CRT's replacement for a
40 round_trip({"plain", "with space"}, "plain and spaced arguments"); 43// character the code page lacks), not a crash.
41 round_trip({"C:\\My Games\\", "next"}, "a quoted argument ending in a backslash"); 44void check_non_ascii(const std::wstring& want, const char* what) {
42 round_trip({"a\\\"b"}, "a backslash before a quote"); 45 round_trip({want}, what);
43 round_trip({"\\\\server\\share\\x y"}, "a UNC path with a space"); 46 if (!wargv()[1] || want != wargv()[1]) {
44 47 std::fprintf(stderr, "FAIL %s: __wargv[1] is not the argument passed\n", what);
45 // main.cpp passes game arguments as std::filesystem::path::string(): ANSI code page text, not UTF-8. 48 ++failures;
46 const std::wstring cafe = L"caf\u00e9"; 49 }
47 round_trip({std::filesystem::path(cafe).string()}, "a non-ASCII argument"); 50 char expected[64] = {};
48 if (!wargv()[1] || cafe != wargv()[1]) { 51 WideCharToMultiByte(CP_ACP, 0, want.c_str(), -1, expected, sizeof(expected), nullptr, nullptr);
49 std::fprintf(stderr, "FAIL: __wargv[1] is not L\"caf\\u00e9\"\n"); 52 if (!argv()[1] || std::string(expected) != argv()[1]) {
53 std::fprintf(stderr, "FAIL %s: argv[1] is not the code page %u text of the argument\n", what, GetACP());
50 ++failures; 54 ++failures;
51 } 55 }
56}
57} // namespace
58
59int main() {
60 round_trip({L"plain", L"with space"}, "plain and spaced arguments");
61 round_trip({L"C:\\My Games\\", L"next"}, "a quoted argument ending in a backslash");
62 round_trip({L"a\\\"b"}, "a backslash before a quote");
63 round_trip({L"\\\\server\\share\\x y"}, "a UNC path with a space");
64
65 // In code page 1252 but not 1253 (Greek), where path::string() used to throw (RT-84).
66 check_non_ascii(L"caf\u00e9", "a Latin-1 argument");
67 // Greek, Cyrillic and CJK together: no single-byte code page holds all of them, so this covers every PC.
68 check_non_ascii(L"\u0394\u0416\u6F22 x", "an argument outside any single-byte code page");
52 69
53 if (failures) return 1; 70 if (failures) return 1;
54 std::printf("process_cmdline: GetCommandLineW parses back to __wargv, non-ASCII arguments intact\n"); 71 std::printf("process_cmdline: GetCommandLineW parses back to __wargv, non-ASCII arguments intact (code page %u)\n",
72 GetACP());
55 return 0; 73 return 0;
56} 74}