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.cpp54
1 files changed, 54 insertions, 0 deletions
diff --git a/runtime/test/process_cmdline_test.cpp b/runtime/test/process_cmdline_test.cpp
index bf55821d1..f30d4607f 100644
--- a/runtime/test/process_cmdline_test.cpp
+++ b/runtime/test/process_cmdline_test.cpp
@@ -218,6 +218,59 @@ void check_getmainargs_environment() {
218 ++failures; 218 ++failures;
219 } 219 }
220} 220}
221
222// __getmainargs/__wgetmainargs's `expand` (the CRT's _dowildcard): nonzero expands wildcards in the arguments, as
223// _configure_*_argv's mode 2 does; zero leaves them as typed. A null argc or argv fails instead of being written
224// through. Synthetic files in a temporary folder.
225void check_getmainargs_expand() {
226 using Narrow = int(__cdecl*)(int*, char***, char***, int, void*);
227 using Wide = int(__cdecl*)(int*, wchar_t***, wchar_t***, int, void*);
228 const auto narrow_fn = reinterpret_cast<Narrow>(rt::shims::find("msvcrt.dll", "__getmainargs"));
229 const auto wide_fn = reinterpret_cast<Wide>(rt::shims::find("msvcrt.dll", "__wgetmainargs"));
230 if (!narrow_fn || !wide_fn) return; // check_getmainargs_environment reports it
231 wchar_t temp[MAX_PATH]{};
232 if (!GetTempPathW(MAX_PATH, temp)) {
233 std::fprintf(stderr, "FAIL GetTempPathW\n");
234 ++failures;
235 return;
236 }
237 const std::wstring dir = std::wstring(temp) + L"leonida_getmainargs_" + std::to_wstring(GetCurrentProcessId());
238 CreateDirectoryW(dir.c_str(), nullptr);
239 const std::wstring files[] = {dir + L"\\a1.lx", dir + L"\\a2.lx"};
240 for (const auto& f : files) {
241 const HANDLE h = CreateFileW(f.c_str(), GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
242 if (h != INVALID_HANDLE_VALUE) CloseHandle(h);
243 }
244 const std::wstring pattern = dir + L"\\*.lx";
245 rt::process::set_command_line(L"game.exe", {pattern});
246 for (const int expand : {0, 1}) {
247 const int want = expand ? 3 : 2; // game.exe and the two files, or game.exe and the pattern
248 int argc = 0;
249 char** argv = nullptr;
250 if (narrow_fn(&argc, &argv, nullptr, expand, nullptr) != 0 || argc != want || !argv ||
251 (!expand && std::strchr(argv[1], '*') == nullptr) || (expand && std::strchr(argv[1], '*') != nullptr)) {
252 std::fprintf(stderr, "FAIL __getmainargs(expand=%d): argc %d, want %d\n", expand, argc, want);
253 ++failures;
254 }
255 int wargc = 0;
256 wchar_t** wargv = nullptr;
257 if (wide_fn(&wargc, &wargv, nullptr, expand, nullptr) != 0 || wargc != want || !wargv ||
258 (!expand && std::wcschr(wargv[1], L'*') == nullptr) || (expand && std::wcschr(wargv[1], L'*') != nullptr)) {
259 std::fprintf(stderr, "FAIL __wgetmainargs(expand=%d): argc %d, want %d\n", expand, wargc, want);
260 ++failures;
261 }
262 }
263 int argc = 0;
264 char** argv = nullptr;
265 wchar_t** wargv = nullptr;
266 if (narrow_fn(nullptr, &argv, nullptr, 0, nullptr) == 0 || narrow_fn(&argc, nullptr, nullptr, 0, nullptr) == 0 ||
267 wide_fn(nullptr, &wargv, nullptr, 0, nullptr) == 0 || wide_fn(&argc, nullptr, nullptr, 0, nullptr) == 0) {
268 std::fprintf(stderr, "FAIL __getmainargs/__wgetmainargs accepted a null argc or argv\n");
269 ++failures;
270 }
271 for (const auto& f : files) DeleteFileW(f.c_str());
272 RemoveDirectoryW(dir.c_str());
273}
221} // namespace 274} // namespace
222 275
223int main() { 276int main() {
@@ -234,6 +287,7 @@ int main() {
234 check_import_names(); 287 check_import_names();
235 check_argv_reset(); 288 check_argv_reset();
236 check_getmainargs_environment(); 289 check_getmainargs_environment();
290 check_getmainargs_expand();
237 check_winmain(L"game.exe", {L"-foo", L"with space"}, L"-foo \"with space\"", "arguments after a plain name"); 291 check_winmain(L"game.exe", {L"-foo", L"with space"}, L"-foo \"with space\"", "arguments after a plain name");
238 check_winmain(L"C:\\My Games\\game.exe", {L"-x"}, L"-x", "a quoted program name with a space"); 292 check_winmain(L"C:\\My Games\\game.exe", {L"-x"}, L"-x", "a quoted program name with a space");
239 check_winmain(L"game.exe", {}, L"", "no arguments"); 293 check_winmain(L"game.exe", {}, L"", "no arguments");