Run macOS applications on your Windows desktop, with a background runtime and integrated app windows.
C++
#include "wsm/app_registry.hpp"
#include "wsm/guest_app_path.hpp"
#include "wsm/runtime_password.hpp"
#include "wsm/setup_assistant.hpp"
#include "wsm/setup_memory.hpp"
#include "wsm/qemu_extra_args.hpp"
#include "wsm/recovery_script.hpp"
#include "wsm/setup_resume.hpp"
#include "wsm/diagnostic_output.hpp"
#include "wsm/reims_registry.hpp"
#include "wsm/ssh_private_key.hpp"
#include "wsm/qmp_unix.hpp"
#include "wsm/ssh_tools.hpp"
#include "wsm/process_identity.hpp"
#include "wsm/vm_health.hpp"
#include "wsm/runtime_idle.hpp"
#include "wsm/runtime_desktop.hpp"
#include "wsm/window_pixels.hpp"
#include "wsm/input_batches.hpp"
#include "wsm/window_focus.hpp"
#include <windows.h>
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <vector>
namespace wsm::runtime {
bool change_runtime_password_with_runner(const std::string&, const std::string&, std::string&,
int (*)(const std::string&, const std::string&));
bool create_app_shortcuts_in(const std::string&, const std::string&, const std::string&,
const std::filesystem::path&, const std::filesystem::path&, std::string&);
}
namespace fs = std::filesystem;
static int guest_result = 0;
static int probe_result = 0;
static std::string captured_script;
int fake_guest(const std::string&, const std::string& script) {
if (script == "exit 0\n") return probe_result;
captured_script = script;
if (const char* capture = std::getenv("WSM_TEST_CAPTURE_PATH")) std::ofstream(capture, std::ios::binary) << script;
return guest_result;
}
#define CHECK(x) do { if (!(x)) { std::cerr << "Failed at line " << __LINE__ << ": " #x "\n"; return 1; } } while (0)
int main() {
using namespace wsm::runtime;
const FocusBounds focus_target{100,100,500,400,0};
CHECK(!guest_point_covered(200,108,0,{{400,200,500,400,1}}));
CHECK(guest_point_covered(200,108,0,{{100,100,500,400,1}}));
CHECK((visible_title_point(focus_target, {}, 1920,1080) == std::pair<int,int>{350,108}));
CHECK(!visible_title_point(focus_target, {{0,0,1920,1080,1}},1920,1080));
const auto exposed = visible_title_point(focus_target, {{300,100,200,400,1}},1920,1080);
CHECK(exposed && !guest_point_covered(exposed->first, exposed->second,0,{{300,100,200,400,1}}));
CHECK(gui_open_command("com.apple.Safari").find("asuser") != std::string::npos);
CHECK(gui_open_command("/Applications/Alice's App.app").find("'\\''") != std::string::npos);
const auto clicks = synchronized_input_batches({"Mold-position", "Mclick-position", "Eleft-down", "Eleft-up", "Eright-down", "Eright-up"});
CHECK(clicks.size() == 4 && clicks[0] == "click-position,left-down");
CHECK(clicks[1] == "left-up" && clicks[2] == "right-down" && clicks[3] == "right-up");
CHECK(synchronized_input_batches({"Mfirst", "Mlast"}) == std::vector<std::string>{"last"});
CHECK(input_key_code("{\"key\":{\"type\":\"qcode\",\"data\":\"a\"}}") == "a");
CHECK(input_key_code("{\"type\":\"btn\"}").empty());
CHECK(is_guest_app_path("/System/Library/CoreServices/Finder.app"));
CHECK(!is_guest_app_path("/System/Library/CoreServices/../Other.app"));
InputCadence cadence;
const std::string key_a = "{\"key\":{\"type\":\"qcode\",\"data\":\"a\"}}";
CHECK(cadence.wait_ms(key_a, 100) == 0);
cadence.delivered(key_a, 100);
CHECK(cadence.wait_ms(key_a, 102) == 8);
CHECK(cadence.wait_ms(key_a, 110) == 0);
CHECK(cadence.wait_ms(key_a, 300) == 0); // normal typing never sleeps
CHECK(cadence.wait_ms("{\"type\":\"abs\"}", 102) == 0);
CHECK(cadence.wait_ms("{\"down\":true}", 102) == 0); // pointer independent of keyboard
CHECK(released_input_events({"a"}).find("\"down\":true") == std::string::npos);
CHECK(released_input_events({"a"}).find("\"data\":\"a\"") != std::string::npos);
std::vector<uint8_t> corner_pixels(80 * 60 * 4, 255);
round_window_pixels(corner_pixels.data(), 80, 60, 24);
CHECK(corner_pixels[3] == 0 && corner_pixels[(80 * 60 - 1) * 4 + 3] == 0);
CHECK(corner_pixels[(30 * 80 + 40) * 4 + 3] == 255);
std::vector<uint8_t> dark_pixels(80 * 60 * 4, 0);
for (size_t i = 3; i < dark_pixels.size(); i += 4) dark_pixels[i] = 255;
round_window_pixels(dark_pixels.data(), 80, 60, 24);
CHECK(dark_pixels[(24 * 80 + 24) * 4 + 3] == 255);
for (size_t i = 0; i < corner_pixels.size(); i += 4) CHECK(corner_pixels[i] <= corner_pixels[i+3]);
CHECK(desktop_elapsed_seconds(" 03:01\n") == 181);
CHECK(desktop_elapsed_seconds("01:02:03") == 3723);
CHECK(desktop_elapsed_seconds("2-00:00:01") == 172801);
CHECK(desktop_elapsed_seconds("bad") == -1);
CHECK(desktop_elapsed_seconds("00:99") == -1);
CHECK(!account_recovery_due(29)); CHECK(account_recovery_due(30));
CHECK(!window_watcher_timed_out(119999, false)); CHECK(window_watcher_timed_out(120000, false));
CHECK(!window_watcher_timed_out(14999, true)); CHECK(window_watcher_timed_out(15000, true));
CHECK(!runtime_idle_ready(true, false, 0, false, 900000)); // SSH/old session alone is not an idle desktop.
CHECK(!runtime_idle_ready(false, true, 0, false, 900000));
CHECK(!runtime_idle_ready(true, true, 1, false, 900000));
CHECK(!runtime_idle_ready(true, true, 0, true, 900000));
CHECK(!runtime_idle_ready(true, true, 0, false, 180000));
CHECK(runtime_idle_ready(true, true, 0, false, 180001));
CHECK(!compact_requested("after setup\n"));
CHECK(compact_requested("asked\n"));
CHECK(!maintenance_can_shutdown(3, 4, true, 0, false)); // A launch during maintenance cancels shutdown.
CHECK(!maintenance_can_shutdown(3, 3, false, 0, false));
CHECK(maintenance_can_shutdown(3, 3, true, 0, false));
const std::string legacy_launch = "19:16:10 guest$ A=/Applications/Install.app; \"$A/Contents/Resources/startosinstall\" --agreetolicense --volume \"$V\"\n";
CHECK(legacy_install_needs_resume(legacy_launch + "22:20:23 [booting] Starting the macOS installer\n22:22:18 [failed] WSM couldn't take control of the macOS installer.\n"));
CHECK(!legacy_install_needs_resume("[booting] Starting the macOS installer\n"));
CHECK(!legacy_install_needs_resume(legacy_launch + "[failed] The macOS installer reported an error before first boot.\n"));
CHECK(!legacy_install_needs_resume(legacy_launch + "[first_boot] Setting up macOS\n"));
CHECK(!legacy_install_needs_resume(legacy_launch + "[downloading] Downloading macOS\n"));
CHECK(recovery_script_cksum("123456789") == 930766865u);
CHECK(recovery_script_cksum("") == 4294967295u);
const auto transfer = recovery_script_transfer(std::string(6000, 'x'), "/tmp/test-script.sh");
CHECK(transfer.size() < 7500);
size_t line_start = 0;
while (line_start < transfer.size()) {
const auto end = transfer.find('\n', line_start);
CHECK((end == std::string::npos ? transfer.size() : end) - line_start < 512);
line_start = end == std::string::npos ? transfer.size() : end + 1;
}
const auto quotes = recovery_script_transfer(std::string(1000, '\''), "/tmp/test-script.sh");
for (size_t start = 0; start < quotes.size();) {
const auto end = quotes.find('\n', start);
CHECK((end == std::string::npos ? quotes.size() : end) - start < 512);
start = end == std::string::npos ? quotes.size() : end + 1;
}
CHECK(!setup_action_clickable(SetupAssistantStep::WelcomePending));
CHECK(!setup_action_clickable(SetupAssistantStep::Unknown));
CHECK(setup_action_clickable(SetupAssistantStep::Welcome));
CHECK(!welcome_restart_due(true, false, 119));
CHECK(welcome_restart_due(true, false, 120));
CHECK(!welcome_restart_due(false, false, 1200));
CHECK(!welcome_restart_due(true, true, 1200));
CHECK(!first_sign_in_restart_due(true, false, 119, 179));
CHECK(first_sign_in_restart_due(true, false, 120, 0));
CHECK(first_sign_in_restart_due(true, false, 0, 180));
CHECK(!first_sign_in_restart_due(false, false, 1200, 1200));
CHECK(!first_sign_in_restart_due(true, true, 1200, 1200));
CHECK(setup_memory_mib(8077) == 4096);
CHECK(qemu_extra_arg("dir=C:/Users/Alice Smith/payload") == "'dir=C:/Users/Alice Smith/payload'");
CHECK(qemu_extra_arg("O'Neil") == "'O'\\''Neil'");
CHECK(qemu_option_path("C:\\Users\\Alice, Smith") == "C:/Users/Alice,, Smith");
CHECK(payload_extra_args("C:/Users/Alice Smith/payload").find("-drive 'if=none,id=wsmpayload,driver=vvfat,dir=C:/Users/Alice Smith/payload,") == 0);
CHECK(setup_memory_mib(16384) == 5120);
CHECK(setup_memory_mib(8077, "memory_mib = 5120\n") == 5120);
CHECK(setup_memory_mib(16384, "memory_mib=8192 # existing runtime\n") == 8192);
CHECK(!setup_commit_available(4096ull * 1024 * 1024, 4096));
CHECK(setup_commit_available(4352ull * 1024 * 1024, 4096));
CHECK(is_guest_app_path("/Applications/Sketch.app"));
CHECK(is_guest_app_path("/Applications/Alice's App (2).app"));
CHECK(is_guest_app_path("/Applications/Glazba Čarobnjak.app"));
CHECK(!is_guest_app_path("/Applications/../tmp/Not.app"));
CHECK(!is_guest_app_path("/Applications/Bad\nName.app"));
CHECK(!is_guest_app_path("/tmp/Not.app"));
CHECK(guest_app_name("/Applications/Alice's App.app") == "Alice's App");
CHECK(guest_shell_quote("a'b") == "'a'\\''b'");
CHECK(guest_command_base64("hello") == "aGVsbG8=");
const struct { const char* file; SetupAssistantStep step; int x, y; } screens[] = {
{"country.ppm", SetupAssistantStep::Country, 0, 0},
{"country_25g342.ppm", SetupAssistantStep::Country, 0, 0},
{"country_tahoe_hidpi.ppm", SetupAssistantStep::Country, 0, 0},
{"update.ppm", SetupAssistantStep::Update, 325, 414},
{"account.ppm", SetupAssistantStep::Account, 335, 401},
{"apple_account.ppm", SetupAssistantStep::AppleAccount, 335, 402},
{"account_menu.ppm", SetupAssistantStep::AccountMenu, 335, 429},
{"account_menu_tahoe.ppm", SetupAssistantStep::AccountMenu, 335, 429},
{"account_confirm.ppm", SetupAssistantStep::AccountConfirm, 509, 331},
{"account_confirm_tahoe.ppm", SetupAssistantStep::AccountConfirm, 509, 331},
{"age_range.ppm", SetupAssistantStep::AgeRange, 480, 382},
{"age_range_tahoe.ppm", SetupAssistantStep::AgeRange, 480, 379},
{"analytics_tahoe.ppm", SetupAssistantStep::Analytics, 637, 404},
{"filevault.ppm", SetupAssistantStep::FileVault, 566, 401},
{"filevault_confirm.ppm", SetupAssistantStep::FileVaultConfirm, 510, 306},
{"filevault_confirm_tahoe.ppm", SetupAssistantStep::FileVaultConfirm, 510, 306},
{"welcome.ppm", SetupAssistantStep::Welcome, 480, 481},
{"welcome_tahoe.ppm", SetupAssistantStep::Welcome, 480, 477},
{"welcome_pending.ppm", SetupAssistantStep::WelcomePending, 0, 0},
{"welcome_pending_tahoe.ppm", SetupAssistantStep::WelcomePending, 0, 0},
{"normal_app.ppm", SetupAssistantStep::Unknown, 0, 0},
};
for (const auto& s : screens) {
std::ifstream f(fs::path(WSM_TEST_FIXTURE_DIR) / s.file, std::ios::binary);
std::string magic; int width=0, height=0, max=0;
f >> magic >> width >> height >> max; f.get();
CHECK(f && magic == "P6" && width == 160 && height == 90 && max == 255);
std::vector<uint8_t> pixels(static_cast<size_t>(width) * height * 3);
f.read(reinterpret_cast<char*>(pixels.data()), static_cast<std::streamsize>(pixels.size()));
CHECK(static_cast<size_t>(f.gcount()) == pixels.size());
const auto action = recognize_setup_assistant(pixels.data(), width, height);
if (action.step != s.step) { std::cerr << "setup fixture mismatch: " << s.file << " got " << static_cast<int>(action.step) << " expected " << static_cast<int>(s.step) << "\n"; return 1; }
CHECK(action.x == s.x * 32767 / 960 && action.y == s.y * 32767 / 540);
if (s.step == SetupAssistantStep::Country) {
for (int offset : {-2, 2}) {
std::vector<uint8_t> shifted(pixels.size(), 193);
for (int y = 4; y < height - 4; ++y) for (int x = 4; x < width - 4; ++x)
for (int c = 0; c < 3; ++c)
shifted[((y + offset) * width + x + offset) * 3 + c] = pixels[(y * width + x) * 3 + c];
CHECK(recognize_setup_assistant(shifted.data(), width, height).step == SetupAssistantStep::Country);
}
}
}
CHECK(recognize_setup_assistant(nullptr, 0, 0).step == SetupAssistantStep::Unknown);
{
auto load_screen = [](const char* name) {
std::ifstream f(fs::path(WSM_TEST_FIXTURE_DIR) / name, std::ios::binary);
std::string magic; int w = 0, h = 0, max = 0; f >> magic >> w >> h >> max; f.get();
std::vector<uint8_t> p(static_cast<size_t>(w) * h * 3);
f.read(reinterpret_cast<char*>(p.data()), static_cast<std::streamsize>(p.size()));
return p;
};
auto parent = load_screen("apple_account.ppm");
const auto menu = load_screen("account_menu.ppm");
CHECK(parent.size() == 160 * 90 * 3 && menu.size() == parent.size());
for (int y = 60; y < 80; ++y) for (int x = 40; x < 75; ++x)
for (int c = 0; c < 3; ++c) parent[(y * 160 + x) * 3 + c] = menu[(y * 160 + x) * 3 + c];
CHECK(recognize_setup_assistant(parent.data(), 160, 90).step == SetupAssistantStep::AccountMenu);
// Centered-card cross-resolution tests: macOS Setup Assistant draws its 800x600 card at
// fixed 1x pixel size centered on any guest resolution (e.g. 1744x984 vs 1818x1033 vs 1920x1080 vs 2560x1440).
auto project_centered = [](const std::vector<uint8_t>& src160, int src_w, int src_h, int dst_w, int dst_h,
int shift_x = 0, int shift_y = 0) {
std::vector<uint8_t> dst(static_cast<size_t>(dst_w) * dst_h * 3, 193);
const int ox = (dst_w - src_w) / 2 + shift_x;
const int oy = (dst_h - src_h) / 2 + shift_y;
for (int y = 0; y < 90; ++y) {
const int py0 = oy + y * src_h / 90, py1 = oy + (y + 1) * src_h / 90;
for (int x = 0; x < 160; ++x) {
const int px0 = ox + x * src_w / 160, px1 = ox + (x + 1) * src_w / 160;
const uint8_t* p = &src160[(y * 160 + x) * 3];
for (int py = py0; py < py1; ++py) {
if (py < 0 || py >= dst_h) continue;
for (int px = px0; px < px1; ++px) {
if (px < 0 || px >= dst_w) continue;
const size_t off = (static_cast<size_t>(py) * dst_w + px) * 3;
dst[off] = p[0]; dst[off + 1] = p[1]; dst[off + 2] = p[2];
}
}
}
}
return dst;
};
// 1744x984 templates (Update, AgeRange) recognized on a 1920x1080 display with remapped click targets.
const auto update_on_1080 = project_centered(load_screen("update.ppm"), 1744, 984, 1920, 1080);
const auto update_action = recognize_setup_assistant(update_on_1080.data(), 1920, 1080);
CHECK(update_action.step == SetupAssistantStep::Update);
CHECK(update_action.x > 11300 && update_action.x < 11900 && update_action.y > 24000 && update_action.y < 24800);
const auto age_on_1080 = project_centered(load_screen("age_range.ppm"), 1744, 984, 1920, 1080);
const auto age_action = recognize_setup_assistant(age_on_1080.data(), 1920, 1080);
CHECK(age_action.step == SetupAssistantStep::AgeRange);
CHECK(age_action.x > 16200 && age_action.x < 16600 && age_action.y > 22200 && age_action.y < 23000);
// Windows-taskbar-clamped 1818x1033 display with off-center (+5px vertical, -3px horizontal) card shift.
const auto country_on_1818 = project_centered(load_screen("country_25g342.ppm"), 1920, 1080, 1818, 1033, -3, 5);
CHECK(recognize_setup_assistant(country_on_1818.data(), 1818, 1033).step == SetupAssistantStep::Country);
const auto update_on_1818 = project_centered(load_screen("update.ppm"), 1744, 984, 1818, 1033, -3, 5);
CHECK(recognize_setup_assistant(update_on_1818.data(), 1818, 1033).step == SetupAssistantStep::Update);
// 16:10 (1920x1200) and 21:9 ultrawide (3440x1440) displays with non-16:9 aspect ratios.
const auto country_on_1200 = project_centered(load_screen("country_25g342.ppm"), 1920, 1080, 1920, 1200, 0, 12);
CHECK(recognize_setup_assistant(country_on_1200.data(), 1920, 1200).step == SetupAssistantStep::Country);
const auto country_on_ultrawide = project_centered(load_screen("country_25g342.ppm"), 1920, 1080, 3440, 1440, 0, 10);
CHECK(recognize_setup_assistant(country_on_ultrawide.data(), 3440, 1440).step == SetupAssistantStep::Country);
// 1920x1080 templates (Country 25G342, AppleAccount, AccountMenu popup, FileVault) on 1744x984 & 2560x1440.
const auto country_on_1744 = project_centered(load_screen("country_25g342.ppm"), 1920, 1080, 1744, 984);
CHECK(recognize_setup_assistant(country_on_1744.data(), 1744, 984).step == SetupAssistantStep::Country);
// Unfocused Country screen on 1712x964 where the first list row turns grey (#D4D4D4) instead of blue.
auto country_unfocused_160 = load_screen("country_25g342.ppm");
for (int y = 36; y < 40; ++y) for (int x = 58; x < 102; ++x)
for (int c = 0; c < 3; ++c) country_unfocused_160[(y * 160 + x) * 3 + c] = 212;
const auto country_unfocused_1712 = project_centered(country_unfocused_160, 1920, 1080, 1712, 964);
CHECK(recognize_setup_assistant(country_unfocused_1712.data(), 1712, 964).step == SetupAssistantStep::Country);
const auto apple_on_1744 = project_centered(load_screen("apple_account.ppm"), 1920, 1080, 1744, 984);
CHECK(recognize_setup_assistant(apple_on_1744.data(), 1744, 984).step == SetupAssistantStep::AppleAccount);
const auto popup_on_1744 = project_centered(parent, 1920, 1080, 1744, 984);
CHECK(recognize_setup_assistant(popup_on_1744.data(), 1744, 984).step == SetupAssistantStep::AccountMenu);
const auto fv_on_1440 = project_centered(load_screen("filevault.ppm"), 1920, 1080, 2560, 1440);
CHECK(recognize_setup_assistant(fv_on_1440.data(), 2560, 1440).step == SetupAssistantStep::FileVault);
// 2x HiDPI/Retina 3488x1968 and 3616x2040 displays (1600x1200 card) + 1280x720 small display.
const auto country_on_retina = project_centered(load_screen("country_tahoe_hidpi.ppm"), 3488, 1968, 3488, 1968);
CHECK(recognize_setup_assistant(country_on_retina.data(), 3488, 1968).step == SetupAssistantStep::Country);
const auto age_on_retina = project_centered(load_screen("age_range_tahoe.ppm"), 3616, 2040, 3616, 2040);
CHECK(recognize_setup_assistant(age_on_retina.data(), 3616, 2040).step == SetupAssistantStep::AgeRange);
const auto analytics_on_retina = project_centered(load_screen("analytics_tahoe.ppm"), 3840, 2160, 3616, 2040);
CHECK(recognize_setup_assistant(analytics_on_retina.data(), 3616, 2040).step == SetupAssistantStep::Analytics);
const auto analytics_on_1280 = project_centered(load_screen("analytics_tahoe.ppm"), 1920, 1080, 1280, 800);
CHECK(recognize_setup_assistant(analytics_on_1280.data(), 1280, 800).step == SetupAssistantStep::Analytics);
const auto welcome_tahoe_on_1080 = project_centered(load_screen("welcome_tahoe.ppm"), 1808, 1020, 1920, 1080);
CHECK(recognize_setup_assistant(welcome_tahoe_on_1080.data(), 1920, 1080).step == SetupAssistantStep::Welcome);
// Lake Tahoe backdrop with a blank white pill (no "Get Started" text) must never trigger Welcome.
auto tahoe_blank_pill = project_centered(load_screen("welcome_pending_tahoe.ppm"), 1712, 964, 1712, 964);
for (int y = 829; y < 863; ++y) for (int x = 804; x < 908; ++x)
for (int c = 0; c < 3; ++c) tahoe_blank_pill[(static_cast<size_t>(y) * 1712 + x) * 3 + c] = 250;
CHECK(recognize_setup_assistant(tahoe_blank_pill.data(), 1712, 964).step != SetupAssistantStep::Welcome);
const auto age_tahoe_on_1080 = project_centered(load_screen("age_range_tahoe.ppm"), 1808, 1020, 1920, 1080);
const auto age_tahoe_action = recognize_setup_assistant(age_tahoe_on_1080.data(), 1920, 1080);
CHECK(age_tahoe_action.step == SetupAssistantStep::AgeRange);
CHECK(age_tahoe_action.x > 16200 && age_tahoe_action.x < 16600 && age_tahoe_action.y > 22400 && age_tahoe_action.y < 23000);
const auto apple_on_720 = project_centered(load_screen("apple_account.ppm"), 1920, 1080, 1280, 720);
CHECK(recognize_setup_assistant(apple_on_720.data(), 1280, 720).step == SetupAssistantStep::AppleAccount);
const auto fv_on_retina = project_centered(load_screen("filevault.ppm"), 3840, 2160, 3488, 1968);
CHECK(recognize_setup_assistant(fv_on_retina.data(), 3488, 1968).step == SetupAssistantStep::FileVault);
// Dark-grey (#808080) reduceTransparency footer bar on a 1280x800 FileVault card must still
// restore full 4:3 height and land clicks accurately on "Not Now".
auto fv_grey_footer = project_centered(load_screen("filevault.ppm"), 1920, 1080, 1280, 800);
for (int y = 637; y < 700; ++y) for (int x = 240; x < 1040; ++x)
for (int c = 0; c < 3; ++c) fv_grey_footer[(static_cast<size_t>(y) * 1280 + x) * 3 + c] = 128;
const auto fv_grey_action = recognize_setup_assistant(fv_grey_footer.data(), 1280, 800);
CHECK(fv_grey_action.step == SetupAssistantStep::FileVault);
CHECK(fv_grey_action.y > 26600 && fv_grey_action.y < 27600);
const auto fv_confirm_on_retina = project_centered(load_screen("filevault_confirm_tahoe.ppm"), 3840, 2160, 3488, 1968);
CHECK(recognize_setup_assistant(fv_confirm_on_retina.data(), 3488, 1968).step == SetupAssistantStep::FileVaultConfirm);
// Non-setup screens must still be rejected at full resolution (strict <= 1.5 threshold intact).
const auto normal_on_1080 = project_centered(load_screen("normal_app.ppm"), 1920, 1080, 1920, 1080);
CHECK(recognize_setup_assistant(normal_on_1080.data(), 1920, 1080).step == SetupAssistantStep::Unknown);
const auto normal_on_1818 = project_centered(load_screen("normal_app.ppm"), 1920, 1080, 1818, 1033);
CHECK(recognize_setup_assistant(normal_on_1818.data(), 1818, 1033).step == SetupAssistantStep::Unknown);
const auto normal_on_1744 = project_centered(load_screen("normal_app.ppm"), 1744, 984, 1744, 984);
CHECK(recognize_setup_assistant(normal_on_1744.data(), 1744, 984).step == SetupAssistantStep::Unknown);
std::vector<uint8_t> blank_1080(1920 * 1080 * 3, 255);
CHECK(recognize_setup_assistant(blank_1080.data(), 1920, 1080).step == SetupAssistantStep::Unknown);
// Synthetic white centered app window (800x600 solid white on grey) must also be rejected.
std::vector<uint8_t> fake_white_card(1818 * 1033 * 3, 193);
for (int y = 216; y < 816; ++y) for (int x = 509; x < 1309; ++x)
for (int c = 0; c < 3; ++c) fake_white_card[(static_cast<size_t>(y) * 1818 + x) * 3 + c] = 255;
CHECK(recognize_setup_assistant(fake_white_card.data(), 1818, 1033).step == SetupAssistantStep::Unknown);
}
std::vector<uint8_t> blank(160 * 90 * 3, 255);
CHECK(recognize_setup_assistant(blank.data(), 160, 90).step == SetupAssistantStep::Unknown);
if (const char* full_dir = std::getenv("WSM_SETUP_FULL_FRAME_DIR")) {
for (const auto& s : screens) {
std::ifstream f(fs::path(full_dir) / s.file, std::ios::binary);
std::string magic; int w = 0, h = 0, max = 0;
f >> magic >> w >> h >> max; f.get();
CHECK(f && magic == "P6" && w == 960 && h == 540 && max == 255);
std::vector<uint8_t> pixels(static_cast<size_t>(w) * h * 3);
f.read(reinterpret_cast<char*>(pixels.data()), static_cast<std::streamsize>(pixels.size()));
CHECK(static_cast<size_t>(f.gcount()) == pixels.size());
CHECK(recognize_setup_assistant(pixels.data(), w, h).step == s.step);
}
}
const fs::path root = fs::temp_directory_path() / ("wsm-app-sync-test-" + std::to_string(GetCurrentProcessId()));
CHECK(!fs::exists(root));
const auto standalone_ssh = root / "Standalone OpenSSH";
fs::create_directories(standalone_ssh);
for (auto name : {"ssh.exe", "scp.exe", "ssh-keygen.exe"}) { std::ofstream(standalone_ssh / name) << "test fixture"; }
CHECK(find_ssh_tool("ssh", {root / "missing", standalone_ssh}) == fs::absolute(standalone_ssh / "ssh.exe"));
CHECK(find_ssh_tool("scp", {standalone_ssh}) == fs::absolute(standalone_ssh / "scp.exe"));
CHECK(find_ssh_tool("ssh-keygen", {standalone_ssh}) == fs::absolute(standalone_ssh / "ssh-keygen.exe"));
CHECK(find_ssh_tool("../ssh", {standalone_ssh}).empty());
CHECK(find_ssh_tool("ssh", {root / "missing"}).empty());
wchar_t own_executable[32768]{};
CHECK(GetModuleFileNameW(nullptr, own_executable, 32768) != 0);
CHECK(process_runs_executable(GetCurrentProcessId(), own_executable));
CHECK(!process_runs_executable(GetCurrentProcessId(), standalone_ssh / "ssh.exe"));
CHECK(!process_runs_executable(0, own_executable));
CHECK(qmp_status_name("{\"return\":{\"status\": \"paused\",\"running\":false}}") == "paused");
CHECK(qmp_status_name("{\"return\":{\"status\":\"running\"}}") == "running");
CHECK(qmp_status_name("{\"error\":{}}").empty());
CHECK(unexpected_setup_stop("paused"));
CHECK(unexpected_setup_stop("guest-panicked"));
CHECK(!unexpected_setup_stop("running"));
CHECK(!unexpected_setup_stop("shutdown"));
for (auto name : {"ssh.exe", "scp.exe", "ssh-keygen.exe"}) fs::remove(standalone_ssh / name);
fs::remove(standalone_ssh);
fs::create_directories(root / "runtime" / "config" / "guest");
CHECK(!wsm::core::QmpUnixClient((root / "missing-qmp.sock").string()).type_ascii_text("test"));
const auto ssh_key = root / "runtime" / "config" / "guest" / "id_ed25519";
CHECK(ssh_private_key_path(root.string()).empty());
{ std::ofstream(ssh_key) << "test key"; }
CHECK(ssh_private_key_path(root.string()) == ssh_key.string());
CHECK(restrict_to_current_account(ssh_key));
char previous_local[MAX_PATH]{};
const DWORD previous_len = GetEnvironmentVariableA("LOCALAPPDATA", previous_local, MAX_PATH);
CHECK(previous_len > 0 && previous_len < MAX_PATH);
CHECK(SetEnvironmentVariableA("LOCALAPPDATA", root.string().c_str()));
const std::string copy = ssh_private_key_path_impl(root.string(), true);
CHECK(!copy.empty() && fs::path(copy) != ssh_key && same_key_contents(ssh_key, copy));
const auto root2 = root / "second-install";
fs::create_directories(root2 / "runtime" / "config" / "guest");
const auto key2 = root2 / "runtime" / "config" / "guest" / "id_ed25519";
{ std::ofstream(key2) << "different key"; }
const std::string copy2 = ssh_private_key_path_impl(root2.string(), true);
CHECK(!copy2.empty() && copy2 != copy && same_key_contents(key2, copy2));
CHECK(SetEnvironmentVariableA("LOCALAPPDATA", previous_local));
fs::remove(fs::path(copy)); fs::remove(fs::path(copy).parent_path());
fs::remove(fs::path(copy2)); fs::remove(fs::path(copy2).parent_path());
fs::remove(root / "WSM" / "ssh"); fs::remove(root / "WSM");
fs::remove(key2); fs::remove(key2.parent_path()); fs::remove(key2.parent_path().parent_path());
fs::remove(key2.parent_path().parent_path().parent_path()); fs::remove(root2);
fs::remove(ssh_key);
CHECK(should_isolate_reims_registry(root));
const auto vm_dir = root / "runtime" / "reims-ctl" / "vms" / "wsm-default";
fs::create_directories(vm_dir);
{ std::ofstream(vm_dir / "vm.toml") << "legacy"; }
CHECK(!should_isolate_reims_registry(root));
const auto isolated_registry = root / "runtime" / "config" / "reims-ctl";
fs::create_directories(isolated_registry);
CHECK(should_isolate_reims_registry(root));
fs::remove(vm_dir / "vm.toml"); fs::remove(vm_dir); fs::remove(vm_dir.parent_path());
fs::remove(vm_dir.parent_path().parent_path()); fs::remove(isolated_registry);
const auto redirected = root / "redirected-desktop";
fs::create_directories(redirected);
std::string report_path;
CHECK(write_diagnostic_report({root / "missing-desktop", redirected}, "report.txt", "setup log content", report_path));
CHECK(fs::exists(redirected / "report.txt"));
{ std::ifstream in(redirected / "report.txt"); std::string content; std::getline(in, content); CHECK(content == "setup log content"); }
CHECK(!write_diagnostic_report({root / "missing-desktop"}, "report.txt", "x", report_path));
const auto programs = root / "start", desktop = root / "desktop";
fs::create_directories(programs); fs::create_directories(desktop);
{ std::ofstream(programs / "Sketch.lnk") << "existing Windows shortcut";
std::ofstream(desktop / "Sketch.lnk") << "existing Windows shortcut"; }
std::string link;
CHECK(create_app_shortcuts_in("Sketch", "com.example.sketch", "", programs, desktop, link));
CHECK(fs::exists(programs / "Sketch (macOS).lnk"));
CHECK(fs::exists(desktop / "Sketch (macOS).lnk"));
{ std::ifstream f(desktop / "Sketch.lnk"); std::string content; std::getline(f, content); CHECK(content == "existing Windows shortcut"); }
CHECK(create_app_shortcuts_in("Sketch", "com.example.sketch", "", programs, desktop, link));
CHECK(!fs::exists(desktop / "Sketch (macOS 2).lnk"));
CHECK(create_app_shortcuts_in("Čarobnjak", "com.example.unicode", "", programs, desktop, link));
CHECK(fs::exists(desktop / L"Čarobnjak.lnk"));
const auto db = (root / "apps.json").string();
AppRegistry apps(db);
AppBundleInfo app; app.id = "com.example.app"; app.name = "An \"App\" {Example}";
app.bundle_path = "/Applications/Alice's App.app"; app.executable_path = app.bundle_path;
apps.upsert(app); CHECK(apps.save());
AppRegistry reopened(db); CHECK(reopened.load());
AppBundleInfo found; CHECK(reopened.find(app.id, found));
CHECK(found.name == app.name && found.bundle_path == app.bundle_path);
const fs::path password_file = root / "runtime" / "config" / "guest" / "password";
const fs::path pending_file = root / "runtime" / "config" / "guest" / "password.pending";
{ std::ofstream f(password_file); f << "original123"; }
std::string error, value;
CHECK(!change_runtime_password_with_runner(root.string(), "short", error, fake_guest));
CHECK(!fs::exists(pending_file));
probe_result = 255; error.clear();
CHECK(!change_runtime_password_with_runner(root.string(), "new password 123", error, fake_guest));
CHECK(!fs::exists(pending_file));
probe_result = 0;
guest_result = 11; error.clear();
CHECK(!change_runtime_password_with_runner(root.string(), "new password 123", error, fake_guest));
CHECK(!fs::exists(pending_file));
CHECK(read_runtime_password(root.string(), value, error) && value == "original123");
guest_result = 255; error.clear();
CHECK(!change_runtime_password_with_runner(root.string(), "new password 123", error, fake_guest));
CHECK(fs::exists(pending_file));
CHECK(!read_runtime_password(root.string(), value, error));
CHECK(captured_script.find("new password 123") == std::string::npos);
fs::remove(pending_file); // this isolated test simulates resolving an interrupted change
guest_result = 0; error.clear();
CHECK(change_runtime_password_with_runner(root.string(), "new password 123", error, fake_guest));
CHECK(!fs::exists(pending_file));
CHECK(read_runtime_password(root.string(), value, error) && value == "new password 123");
fs::remove(programs / "Sketch.lnk"); fs::remove(programs / "Sketch (macOS).lnk");
fs::remove(desktop / "Sketch.lnk"); fs::remove(desktop / "Sketch (macOS).lnk");
fs::remove(programs / L"Čarobnjak.lnk"); fs::remove(desktop / L"Čarobnjak.lnk");
fs::remove(programs); fs::remove(desktop);
fs::remove(redirected / "report.txt"); fs::remove(redirected);
fs::remove(password_file); fs::remove(root / "apps.json");
fs::remove(root / "runtime" / "config" / "guest"); fs::remove(root / "runtime" / "config");
fs::remove(root / "runtime"); fs::remove(root);
std::cout << "App paths, registry and password transaction tests passed.\n";
return 0;
}