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++

Fix QMP ASCII typing reliability and macOS Tahoe disk verification

Konstantin Pertsevaccc85a14274
diff --git a/src/wsm-core/include/wsm/qmp_unix.hpp b/src/wsm-core/include/wsm/qmp_unix.hpp
index 277bc56..115a136 100644
--- a/src/wsm-core/include/wsm/qmp_unix.hpp
+++ b/src/wsm-core/include/wsm/qmp_unix.hpp
@@ -1,10 +1,22 @@
#pragma once
#include <cstdint>
#include <string>
+#include <utility>
#include <vector>
namespace wsm::core {
+struct QmpKeySpec {
+ bool shift = false;
+ const char* qcode = nullptr;
+};
+
+// Maps a printable ASCII byte (plus '\n', '\t') to a QEMU QKeyCode and Shift requirement.
+bool ascii_to_qmp_key(char c, QmpKeySpec& out);
+
+// Builds an input-send-event QMP command from (qcode, down) pairs.
+std::string build_input_key_event_cmd(const std::vector<std::pair<std::string, bool>>& events);
+
// Native Windows Winsock AF_UNIX QMP client for reims-ctl / QEMU unix: sockets.
// Enables headless screendump capture, input-send-event injection, and VM control
// directly inside WSM Manager without opening a separate Reims or QEMU window.
@@ -17,7 +29,8 @@ public:
bool capture_screendump_ppm(const std::string& out_ppm_path, std::string& err) const;
bool send_mouse_click(int32_t abs_x_32k, int32_t abs_y_32k, const std::string& button = "left") const;
bool send_key_qcode(const std::string& qcode) const;
- bool type_ascii_text(const std::string& text) const;
+ bool send_key_combo(const std::vector<std::string>& qcodes, uint32_t hold_ms = 30) const;
+ bool type_ascii_text(const std::string& text, uint32_t hold_ms = 20, uint32_t gap_ms = 45) const;
private:
std::string socket_path_;
diff --git a/src/wsm-core/src/qmp_unix.cpp b/src/wsm-core/src/qmp_unix.cpp
index 1c42b63..04b0608 100644
--- a/src/wsm-core/src/qmp_unix.cpp
+++ b/src/wsm-core/src/qmp_unix.cpp
@@ -15,83 +15,179 @@
namespace wsm::core {
-QmpUnixClient::QmpUnixClient(std::string socket_path) : socket_path_(std::move(socket_path)) {
- if (socket_path_.empty()) {
- // WSM_QMP_SOCKET (dev aid): drive another VM, e.g. a test copy, with the same tools.
- char env[MAX_PATH] = {};
- DWORD n = GetEnvironmentVariableA("WSM_QMP_SOCKET", env, MAX_PATH);
- socket_path_ = (n > 0 && n < MAX_PATH) ? std::string(env, n)
- : resolve_wsm_root() + "\\runtime\\reims-ctl\\vms\\wsm-default\\qmp.sock";
- }
-}
+namespace {
-bool QmpUnixClient::execute(const std::string& json_cmd, std::string& response, uint32_t timeout_ms) const {
- WSADATA wsa{};
- if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) return false;
+class QmpUnixConn {
+public:
+ QmpUnixConn() = default;
+ ~QmpUnixConn() { close(); }
+ QmpUnixConn(const QmpUnixConn&) = delete;
+ QmpUnixConn& operator=(const QmpUnixConn&) = delete;
+
+ bool open(const std::string& socket_path, uint32_t timeout_ms) {
+ WSADATA wsa{};
+ if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) return false;
+ wsa_started_ = true;
+
+ sock_ = socket(AF_UNIX, SOCK_STREAM, 0);
+ if (sock_ == INVALID_SOCKET) {
+ close();
+ return false;
+ }
- SOCKET s = socket(AF_UNIX, SOCK_STREAM, 0);
- if (s == INVALID_SOCKET) {
- WSACleanup();
- return false;
- }
+ DWORD to = timeout_ms;
+ setsockopt(sock_, SOL_SOCKET, SO_RCVTIMEO, reinterpret_cast<const char*>(&to), sizeof(to));
+ setsockopt(sock_, SOL_SOCKET, SO_SNDTIMEO, reinterpret_cast<const char*>(&to), sizeof(to));
- DWORD to = timeout_ms;
- setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, reinterpret_cast<const char*>(&to), sizeof(to));
- setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, reinterpret_cast<const char*>(&to), sizeof(to));
+ SOCKADDR_UN addr{};
+ addr.sun_family = AF_UNIX;
+ if (socket_path.size() >= sizeof(addr.sun_path)) {
+ close();
+ return false;
+ }
+ std::memcpy(addr.sun_path, socket_path.c_str(), socket_path.size() + 1);
- SOCKADDR_UN addr{};
- addr.sun_family = AF_UNIX;
- if (socket_path_.size() >= sizeof(addr.sun_path)) {
- closesocket(s);
- WSACleanup();
- return false;
+ if (connect(sock_, reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) != 0) {
+ close();
+ return false;
+ }
+
+ std::string line;
+ if (!read_line(line)) { // QMP greeting
+ close();
+ return false;
+ }
+
+ std::string resp;
+ if (!command("{\"execute\":\"qmp_capabilities\"}", resp)) {
+ close();
+ return false;
+ }
+ return true;
}
- std::memcpy(addr.sun_path, socket_path_.c_str(), socket_path_.size() + 1);
- if (connect(s, reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) != 0) {
- closesocket(s);
- WSACleanup();
+ bool command(const std::string& json_cmd, std::string& response) {
+ if (sock_ == INVALID_SOCKET) return false;
+ std::string payload = json_cmd + "\n";
+ if (send(sock_, payload.c_str(), static_cast<int>(payload.size()), 0) <= 0) return false;
+
+ std::string line;
+ for (int i = 0; i < 16; ++i) {
+ if (!read_line(line)) return false;
+ if (line.find("\"return\"") != std::string::npos || line.find("\"error\"") != std::string::npos) {
+ response = line;
+ return line.find("\"return\"") != std::string::npos;
+ }
+ }
return false;
}
- auto read_line = [&](std::string& line) -> bool {
+ void close() {
+ if (sock_ != INVALID_SOCKET) {
+ closesocket(sock_);
+ sock_ = INVALID_SOCKET;
+ }
+ if (wsa_started_) {
+ WSACleanup();
+ wsa_started_ = false;
+ }
+ }
+
+private:
+ bool read_line(std::string& line) {
line.clear();
char ch = 0;
- while (recv(s, &ch, 1, 0) == 1) {
+ while (recv(sock_, &ch, 1, 0) == 1) {
if (ch == '\n') return true;
if (ch != '\r') line.push_back(ch);
if (line.size() > 65536) break;
}
return !line.empty();
+ }
+
+ SOCKET sock_ = INVALID_SOCKET;
+ bool wsa_started_ = false;
+};
+
+} // namespace
+
+bool ascii_to_qmp_key(char c, QmpKeySpec& out) {
+ static constexpr const char* kLetters[26] = {
+ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
+ "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"
};
+ static constexpr const char* kDigits[10] = {
+ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"
+ };
+ if (c >= 'a' && c <= 'z') { out = {false, kLetters[c - 'a']}; return true; }
+ if (c >= 'A' && c <= 'Z') { out = {true, kLetters[c - 'A']}; return true; }
+ if (c >= '0' && c <= '9') { out = {false, kDigits[c - '0']}; return true; }
+ switch (c) {
+ case ' ': out = {false, "spc"}; return true;
+ case '-': out = {false, "minus"}; return true;
+ case '_': out = {true, "minus"}; return true;
+ case '=': out = {false, "equal"}; return true;
+ case '+': out = {true, "equal"}; return true;
+ case '/': out = {false, "slash"}; return true;
+ case '?': out = {true, "slash"}; return true;
+ case '\\': out = {false, "backslash"}; return true;
+ case '|': out = {true, "backslash"}; return true;
+ case '.': out = {false, "dot"}; return true;
+ case '>': out = {true, "dot"}; return true;
+ case ',': out = {false, "comma"}; return true;
+ case '<': out = {true, "comma"}; return true;
+ case ';': out = {false, "semicolon"}; return true;
+ case ':': out = {true, "semicolon"}; return true;
+ case '\'': out = {false, "apostrophe"}; return true;
+ case '"': out = {true, "apostrophe"}; return true;
+ case '\n': out = {false, "ret"}; return true;
+ case '\t': out = {false, "tab"}; return true;
+ case '!': out = {true, "1"}; return true;
+ case '@': out = {true, "2"}; return true;
+ case '#': out = {true, "3"}; return true;
+ case '$': out = {true, "4"}; return true;
+ case '%': out = {true, "5"}; return true;
+ case '^': out = {true, "6"}; return true;
+ case '&': out = {true, "7"}; return true;
+ case '*': out = {true, "8"}; return true;
+ case '(': out = {true, "9"}; return true;
+ case ')': out = {true, "0"}; return true;
+ case '[': out = {false, "bracket_left"}; return true;
+ case '{': out = {true, "bracket_left"}; return true;
+ case ']': out = {false, "bracket_right"}; return true;
+ case '}': out = {true, "bracket_right"}; return true;
+ case '`': out = {false, "grave_accent"}; return true;
+ case '~': out = {true, "grave_accent"}; return true;
+ default: return false;
+ }
+}
- std::string line;
- if (!read_line(line)) { // QMP greeting
- closesocket(s);
- WSACleanup();
- return false;
+std::string build_input_key_event_cmd(const std::vector<std::pair<std::string, bool>>& events) {
+ std::string cmd = "{\"execute\":\"input-send-event\",\"arguments\":{\"events\":[";
+ for (size_t i = 0; i < events.size(); ++i) {
+ if (i > 0) cmd += ',';
+ cmd += "{\"type\":\"key\",\"data\":{\"down\":";
+ cmd += (events[i].second ? "true" : "false");
+ cmd += ",\"key\":{\"type\":\"qcode\",\"data\":\"" + events[i].first + "\"}}}";
}
+ cmd += "]}}";
+ return cmd;
+}
- const char* caps = "{\"execute\":\"qmp_capabilities\"}\n";
- send(s, caps, static_cast<int>(std::strlen(caps)), 0);
- read_line(line);
-
- std::string payload = json_cmd + "\n";
- send(s, payload.c_str(), static_cast<int>(payload.size()), 0);
-
- bool ok = false;
- for (int i = 0; i < 8; ++i) {
- if (!read_line(line)) break;
- if (line.find("\"return\"") != std::string::npos || line.find("\"error\"") != std::string::npos) {
- response = line;
- ok = (line.find("\"return\"") != std::string::npos);
- break;
- }
+QmpUnixClient::QmpUnixClient(std::string socket_path) : socket_path_(std::move(socket_path)) {
+ if (socket_path_.empty()) {
+ // WSM_QMP_SOCKET (dev aid): drive another VM, e.g. a test copy, with the same tools.
+ char env[MAX_PATH] = {};
+ DWORD n = GetEnvironmentVariableA("WSM_QMP_SOCKET", env, MAX_PATH);
+ socket_path_ = (n > 0 && n < MAX_PATH) ? std::string(env, n)
+ : resolve_wsm_root() + "\\runtime\\reims-ctl\\vms\\wsm-default\\qmp.sock";
}
+}
- closesocket(s);
- WSACleanup();
- return ok;
+bool QmpUnixClient::execute(const std::string& json_cmd, std::string& response, uint32_t timeout_ms) const {
+ QmpUnixConn conn;
+ if (!conn.open(socket_path_, timeout_ms)) return false;
+ return conn.command(json_cmd, response);
}
bool QmpUnixClient::capture_screendump_ppm(const std::string& out_ppm_path, std::string& err) const {
@@ -126,60 +222,74 @@ bool QmpUnixClient::send_mouse_click(int32_t abs_x_32k, int32_t abs_y_32k, const
}
bool QmpUnixClient::send_key_qcode(const std::string& qcode) const {
+ return send_key_combo({qcode}, 30);
+}
+
+bool QmpUnixClient::send_key_combo(const std::vector<std::string>& qcodes, uint32_t hold_ms) const {
+ if (qcodes.empty()) return true;
+ QmpUnixConn conn;
+ if (!conn.open(socket_path_, 2000)) return false;
+
+ std::vector<std::pair<std::string, bool>> down_ev, up_ev;
+ down_ev.reserve(qcodes.size());
+ up_ev.reserve(qcodes.size());
+ for (const auto& q : qcodes) down_ev.emplace_back(q, true);
+ for (auto it = qcodes.rbegin(); it != qcodes.rend(); ++it) up_ev.emplace_back(*it, false);
+
std::string resp;
- std::string cmd = "{\"execute\":\"send-key\",\"arguments\":{\"keys\":[{\"type\":\"qcode\",\"data\":\"" + qcode + "\"}],\"hold-time\":30}}";
- return execute(cmd, resp, 1500);
+ if (!conn.command(build_input_key_event_cmd(down_ev), resp)) return false;
+ if (hold_ms > 0) Sleep(hold_ms);
+ return conn.command(build_input_key_event_cmd(up_ev), resp);
}
-bool QmpUnixClient::type_ascii_text(const std::string& text) const {
- auto send_combo = [&](bool shift, const std::string& qcode) {
- std::string resp;
- std::string cmd = "{\"execute\":\"send-key\",\"arguments\":{\"keys\":[";
- if (shift) cmd += "{\"type\":\"qcode\",\"data\":\"shift\"},";
- cmd += "{\"type\":\"qcode\",\"data\":\"" + qcode + "\"}],\"hold-time\":25}}";
- execute(cmd, resp, 1500);
- Sleep(35);
+bool QmpUnixClient::type_ascii_text(const std::string& text, uint32_t hold_ms, uint32_t gap_ms) const {
+ if (text.empty()) return true;
+ QmpUnixConn conn;
+ if (!conn.open(socket_path_, 2000)) return false;
+
+ std::string resp;
+ auto send_single = [&](const std::string& qcode, bool down) -> bool {
+ return conn.command(build_input_key_event_cmd({{qcode, down}}), resp);
};
+ // Release any modifier left stuck by an earlier shortcut or failed typing attempt.
+ if (!conn.command(build_input_key_event_cmd({
+ {"shift", false}, {"shift_r", false},
+ {"ctrl", false}, {"ctrl_r", false},
+ {"meta_l", false}, {"meta_r", false}}), resp)) {
+ return false;
+ }
+ if (hold_ms > 0) Sleep(15);
+
+ // Use explicit input-send-event down/up over one connection (rather than send-key's
+ // async hold-time timer) and pace below usb-kbd's 10 ms HID poll interval so its
+ // 16-entry queue never overflows and drops key-up / shift-up events under WHPX jitter.
+ bool shift_held = false;
for (char c : text) {
- if (c >= 'a' && c <= 'z') { send_combo(false, std::string(1, c)); }
- else if (c >= 'A' && c <= 'Z') { send_combo(true, std::string(1, static_cast<char>(c - 'A' + 'a'))); }
- else if (c >= '0' && c <= '9') { send_combo(false, std::string(1, c)); }
- else if (c == ' ') send_combo(false, "spc");
- else if (c == '-') send_combo(false, "minus");
- else if (c == '_') send_combo(true, "minus");
- else if (c == '=') send_combo(false, "equal");
- else if (c == '+') send_combo(true, "equal");
- else if (c == '/') send_combo(false, "slash");
- else if (c == '\\') send_combo(false, "backslash");
- else if (c == '.') send_combo(false, "dot");
- else if (c == ',') send_combo(false, "comma");
- else if (c == ':') send_combo(true, "semicolon");
- else if (c == ';') send_combo(false, "semicolon");
- else if (c == '&') send_combo(true, "7");
- else if (c == '*') send_combo(true, "8");
- else if (c == '"') send_combo(true, "apostrophe");
- else if (c == '\'') send_combo(false, "apostrophe");
- else if (c == '\n') send_combo(false, "ret");
- else if (c == '\t') send_combo(false, "tab");
- else if (c == '|') send_combo(true, "backslash");
- else if (c == '!') send_combo(true, "1");
- else if (c == '@') send_combo(true, "2");
- else if (c == '#') send_combo(true, "3");
- else if (c == '$') send_combo(true, "4");
- else if (c == '%') send_combo(true, "5");
- else if (c == '^') send_combo(true, "6");
- else if (c == '(') send_combo(true, "9");
- else if (c == ')') send_combo(true, "0");
- else if (c == '<') send_combo(true, "comma");
- else if (c == '>') send_combo(true, "dot");
- else if (c == '?') send_combo(true, "slash");
- else if (c == '[') send_combo(false, "bracket_left");
- else if (c == ']') send_combo(false, "bracket_right");
- else if (c == '{') send_combo(true, "bracket_left");
- else if (c == '}') send_combo(true, "bracket_right");
- else if (c == '`') send_combo(false, "grave_accent");
- else if (c == '~') send_combo(true, "grave_accent");
+ QmpKeySpec spec{};
+ if (!ascii_to_qmp_key(c, spec)) continue;
+ if (spec.shift != shift_held) {
+ if (!send_single("shift", spec.shift)) {
+ if (shift_held) send_single("shift", false);
+ return false;
+ }
+ shift_held = spec.shift;
+ if (hold_ms > 0) Sleep(hold_ms);
+ }
+ if (!send_single(spec.qcode, true)) {
+ if (shift_held) send_single("shift", false);
+ return false;
+ }
+ if (hold_ms > 0) Sleep(hold_ms);
+ if (!send_single(spec.qcode, false)) {
+ if (shift_held) send_single("shift", false);
+ return false;
+ }
+ if (gap_ms > 0) Sleep(gap_ms);
+ }
+ if (shift_held) {
+ if (!send_single("shift", false)) return false;
+ if (gap_ms > 0) Sleep(gap_ms);
}
return true;
}
diff --git a/src/wsm-runtime/src/provisioner.cpp b/src/wsm-runtime/src/provisioner.cpp
index 28d481e..ea7b2f7 100644
--- a/src/wsm-runtime/src/provisioner.cpp
+++ b/src/wsm-runtime/src/provisioner.cpp
@@ -232,11 +232,15 @@ bool Provisioner::guest_shell(const std::string& cmd) {
// Paint neutral first so a stale green from a previous command can't be misread.
std::string line = std::string(kColourFns) + " printf '\\033[0m'; printf '\\033[2J\\033[3J\\033[H'; ( " + cmd + " ) && G || R";
log("guest$ " + cmd);
+ // Clear any partial line or unclosed-quote continuation prompt (>) from an interrupted
+ // command or retry before typing into Terminal.
+ q.send_key_combo({"ctrl", "c"}, 40);
+ Sleep(200);
// Keystrokes reach the guest after type_ascii_text returns, so the previous command's green
// could still be on screen when the next verdict is read, and pass a command that hadn't
// even run yet. Clear to neutral first and wait until the screen really shows no verdict.
// (The leading empty line also absorbs a keystroke lost while Terminal's window comes up.)
- q.type_ascii_text("\nprintf '\\033[0m\\033[2J\\033[3J\\033[H'\n");
+ if (!q.type_ascii_text("\nprintf '\\033[0m\\033[2J\\033[3J\\033[H'\n")) return false;
for (int i = 0; i < 60; ++i) {
std::vector<uint8_t> px; int w = 0, h = 0;
if (screen_dump(px, w, h) && read_verdict(px, w, h) == Verdict::None) break;
@@ -287,9 +291,7 @@ bool Provisioner::open_recovery_terminal() {
for (int attempt = 0; attempt < 4; ++attempt) {
q.send_key_qcode("esc");
Sleep(800);
- std::string r;
- q.execute("{\"execute\":\"send-key\",\"arguments\":{\"keys\":[{\"type\":\"qcode\",\"data\":\"shift\"},"
- "{\"type\":\"qcode\",\"data\":\"meta_l\"},{\"type\":\"qcode\",\"data\":\"t\"}]}}", r, 2000);
+ q.send_key_combo({"shift", "meta_l", "t"}, 100);
Sleep(6000);
if (!terminal_visible()) {
q.send_mouse_click(static_cast<int>(122.0 / 872 * 32767), static_cast<int>(7.0 / 492 * 32767));
@@ -354,7 +356,7 @@ bool Provisioner::step_install() {
// read-only Recovery root, which is what made the old script fail with //Install.XXXX).
const std::string pick_target =
"V=; for c in '/Volumes/Macintosh HD' /Volumes/MacintoshHD; do "
- "diskutil info \"$c\" 2>/dev/null | grep -q 'Read-Only Volume: *No' && V=$c; done; [ -n \"$V\" ]";
+ "diskutil info \"$c\" 2>/dev/null | grep -qE '(Volume Read-Only|Read-Only Volume): *No' && V=$c; done; [ -n \"$V\" ]";
guest_shell(pick_target);
if (wait_verdict(this, dump, 60, nullptr) != Verdict::Green) { fail("The prepared disk wasn't available to the installer."); return false; }
@@ -568,9 +570,7 @@ bool Provisioner::boot_into_recovery() {
uint64_t before = sig, after = 0;
q.send_key_qcode("esc");
Sleep(800);
- std::string r;
- q.execute("{\"execute\":\"send-key\",\"arguments\":{\"keys\":[{\"type\":\"qcode\",\"data\":\"shift\"},"
- "{\"type\":\"qcode\",\"data\":\"meta_l\"},{\"type\":\"qcode\",\"data\":\"t\"}]}}", r, 2000);
+ q.send_key_combo({"shift", "meta_l", "t"}, 100);
Sleep(6000);
signature(after);
bool term = terminal_visible();
diff --git a/tests/wsm_core_tests.cpp b/tests/wsm_core_tests.cpp
index 4a481aa..3b6285d 100644
--- a/tests/wsm_core_tests.cpp
+++ b/tests/wsm_core_tests.cpp
@@ -5,8 +5,10 @@
#include "wsm/app_registry.hpp"
#include "wsm/guest_bridge.hpp"
#include "wsm/shell_integration.hpp"
+#include "wsm/qmp_unix.hpp"
#include <winsock2.h>
+#include <afunix.h>
#include <ws2tcpip.h>
#include <windows.h>
@@ -15,6 +17,7 @@
#include <filesystem>
#include <fstream>
#include <iostream>
+#include <map>
#include <thread>
#include <vector>
@@ -180,6 +183,108 @@ int main() {
WSM_CHECK(std::filesystem::file_size("F:\\WSM\\cache\\tmp\\test.ico") == 22 + 16);
}
- std::cout << "[test] ALL 8 CORE & INTEGRATION TESTS PASSED!\n";
+ std::cout << "[test] 9. QmpUnixClient ASCII typing & input-send-event reliability...\n";
+ {
+ wsm::core::QmpKeySpec spec{};
+ WSM_CHECK(wsm::core::ascii_to_qmp_key('a', spec) && !spec.shift && std::strcmp(spec.qcode, "a") == 0);
+ WSM_CHECK(wsm::core::ascii_to_qmp_key('G', spec) && spec.shift && std::strcmp(spec.qcode, "g") == 0);
+ WSM_CHECK(wsm::core::ascii_to_qmp_key('(', spec) && spec.shift && std::strcmp(spec.qcode, "9") == 0);
+ WSM_CHECK(wsm::core::ascii_to_qmp_key(')', spec) && spec.shift && std::strcmp(spec.qcode, "0") == 0);
+ WSM_CHECK(wsm::core::ascii_to_qmp_key(':', spec) && spec.shift && std::strcmp(spec.qcode, "semicolon") == 0);
+ WSM_CHECK(wsm::core::ascii_to_qmp_key(';', spec) && !spec.shift && std::strcmp(spec.qcode, "semicolon") == 0);
+ WSM_CHECK(wsm::core::ascii_to_qmp_key('\n', spec) && !spec.shift && std::strcmp(spec.qcode, "ret") == 0);
+ WSM_CHECK(!wsm::core::ascii_to_qmp_key('\x01', spec));
+
+ WSADATA wsa{};
+ WSM_CHECK(WSAStartup(MAKEWORD(2, 2), &wsa) == 0);
+ std::string sock_path = (std::filesystem::temp_directory_path() /
+ ("wsm_qmp_test_" + std::to_string(GetCurrentProcessId()) + ".sock")).string();
+ std::error_code ec;
+ std::filesystem::remove(sock_path, ec);
+
+ SOCKET srv = socket(AF_UNIX, SOCK_STREAM, 0);
+ WSM_CHECK(srv != INVALID_SOCKET);
+ SOCKADDR_UN addr{};
+ addr.sun_family = AF_UNIX;
+ WSM_CHECK(sock_path.size() < sizeof(addr.sun_path));
+ std::memcpy(addr.sun_path, sock_path.c_str(), sock_path.size() + 1);
+ WSM_CHECK(bind(srv, reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) == 0);
+ WSM_CHECK(listen(srv, 4) == 0);
+
+ std::vector<std::string> commands;
+ std::thread mock_qmp([srv, &commands]() {
+ auto read_line = [](SOCKET c, std::string& line) -> bool {
+ line.clear();
+ char ch = 0;
+ while (recv(c, &ch, 1, 0) == 1) {
+ if (ch == '\n') return true;
+ if (ch != '\r') line.push_back(ch);
+ }
+ return false;
+ };
+ // Serve 2 connections: send_key_combo + type_ascii_text
+ for (int conn_i = 0; conn_i < 2; ++conn_i) {
+ SOCKET cli = accept(srv, nullptr, nullptr);
+ if (cli == INVALID_SOCKET) break;
+ const char* greet = "{\"QMP\":{\"version\":{\"qemu\":{\"micro\":0,\"minor\":2,\"major\":9}},\"capabilities\":[]}}\n";
+ send(cli, greet, static_cast<int>(std::strlen(greet)), 0);
+ std::string line;
+ while (read_line(cli, line)) {
+ commands.push_back(line);
+ const char* ret = "{\"return\":{}}\n";
+ send(cli, ret, static_cast<int>(std::strlen(ret)), 0);
+ }
+ closesocket(cli);
+ }
+ });
+
+ wsm::core::QmpUnixClient qmp(sock_path);
+ WSM_CHECK(qmp.send_key_combo({"ctrl", "c"}, 0));
+
+ const std::string disk_cmd =
+ "if [ -d '/Volumes/Macintosh HD/Applications' ]; then true; else "
+ "T=; for d in $(diskutil list internal physical | grep '^/dev/disk' | cut -d' ' -f1); do "
+ "diskutil list $d | grep -qE 'OPENCORE|Installer|RECOVERY' || T=$d; done; "
+ "[ -n \"$T\" ] && diskutil eraseDisk APFS 'Macintosh HD' GPT $T; fi\n";
+ WSM_CHECK(qmp.type_ascii_text(disk_cmd, 0, 0));
+
+ mock_qmp.join();
+ closesocket(srv);
+ std::filesystem::remove(sock_path, ec);
+ WSACleanup();
+
+ // Verify: only 2 qmp_capabilities handshakes (one for send_key_combo, one for whole type_ascii_text),
+ // zero async send-key calls, and every key down has a matching up with shift left released.
+ int caps_count = 0, send_key_count = 0;
+ bool shift_down = false;
+ std::map<std::string, int> key_balance;
+ for (const auto& cmd : commands) {
+ if (cmd.find("qmp_capabilities") != std::string::npos) ++caps_count;
+ if (cmd.find("\"send-key\"") != std::string::npos) ++send_key_count;
+ if (cmd.find("\"input-send-event\"") == std::string::npos) continue;
+ // Ignore the initial all-modifiers release command (contains shift_r)
+ if (cmd.find("\"shift_r\"") != std::string::npos) continue;
+ size_t pos = 0;
+ while ((pos = cmd.find("\"down\":", pos)) != std::string::npos) {
+ bool down = (cmd.compare(pos + 7, 4, "true") == 0);
+ size_t kpos = cmd.find("\"data\":\"", pos + 7);
+ WSM_CHECK(kpos != std::string::npos);
+ size_t kend = cmd.find('"', kpos + 8);
+ std::string qcode = cmd.substr(kpos + 8, kend - (kpos + 8));
+ key_balance[qcode] += (down ? 1 : -1);
+ if (qcode == "shift") shift_down = down;
+ WSM_CHECK(key_balance[qcode] >= 0 && key_balance[qcode] <= 1);
+ pos = kend;
+ }
+ }
+ WSM_CHECK(caps_count == 2);
+ WSM_CHECK(send_key_count == 0);
+ WSM_CHECK(!shift_down);
+ for (const auto& kv : key_balance) {
+ WSM_CHECK(kv.second == 0);
+ }
+ }
+
+ std::cout << "[test] ALL 9 CORE & INTEGRATION TESTS PASSED!\n";
return 0;
}