Skip to content

armgit / WSM

Public Imported from GitHub
0

Run macOS applications on your Windows desktop, with a background runtime and integrated app windows.

C++
2.6 KB
Raw Blame
#include "wsm/recovery_script.hpp"
#include <windows.h>
#include <filesystem>
#include <fstream>
#include <iostream>

namespace fs = std::filesystem;
bool run(const fs::path& bash, const fs::path& script) {
    std::wstring command = L"\"" + bash.wstring() + L"\" \"" + script.wstring() + L"\"";
    STARTUPINFOW si{}; si.cb = sizeof(si); PROCESS_INFORMATION pi{};
    if (!CreateProcessW(nullptr, command.data(), nullptr, nullptr, FALSE, CREATE_NO_WINDOW, nullptr, nullptr, &si, &pi)) return false;
    const auto wait = WaitForSingleObject(pi.hProcess, 15000);
    if (wait != WAIT_OBJECT_0) TerminateProcess(pi.hProcess, 1);
    DWORD code = 1; GetExitCodeProcess(pi.hProcess, &code);
    CloseHandle(pi.hThread); CloseHandle(pi.hProcess);
    return wait == WAIT_OBJECT_0 && code == 0;
}
int main(int argc, char** argv) {
    if (argc != 2) return 1;
    const auto base = fs::current_path() / ("wsm-transfer-test-" + std::to_string(GetCurrentProcessId()));
    if (fs::exists(base)) return 2;
    fs::create_directory(base);
    const auto target = base / "received-script.sh", marker = base / "executed", harness = base / "test.sh", error_log = base / "copy.log";
    const auto payload = "#!/bin/sh\n# quotes ' and backslashes \\ and percent % and !\n#" + std::string(6000, 'x') + "\nprintf success > " + wsm::runtime::guest_shell_quote(marker.generic_string()) + "\n";
    auto transfer = wsm::runtime::recovery_script_transfer(payload, target.generic_string());
    auto write_harness = [&](const std::string& body) {
        // No external utilities are available during transfer or verification.
        std::ofstream(harness, std::ios::binary) << "export PATH=/nonexistent\n(\n" << body << " &&\nPATH=/usr/bin sh " <<
            wsm::runtime::guest_shell_quote(target.generic_string()) << "\n) > " << wsm::runtime::guest_shell_quote(error_log.generic_string()) << " 2>&1\n";
    };
    write_harness(transfer);
    if (!run(argv[1], harness) || !fs::exists(marker)) return 3;
    fs::remove(marker);
    auto bad = transfer;
    const auto first = bad.find("printf %s '") + 11;
    bad[first] = bad[first] == 'A' ? 'B' : 'A';
    write_harness(bad);
    if (run(argv[1], harness) || fs::exists(marker) || !fs::exists(error_log)) return 4;
    bad = transfer;
    bad.erase(first, 1);
    write_harness(bad);
    if (run(argv[1], harness) || fs::exists(marker) || fs::file_size(error_log) == 0) return 5;
    if (base.parent_path() != fs::current_path() || base.filename().string().rfind("wsm-transfer-test-", 0) != 0) return 6;
    fs::remove_all(base);
    std::cout << "PASS: long script transferred exactly; corrupt/truncated input never executes; preparation errors captured\n";
}