// SPDX-License-Identifier: GPL-3.0-or-later // Standalone native check; invokes an isolated shell without user startup files. #include "DriveWireShell.h" #include #include #include #include #include using VCC::Mac::DriveWireShell; using Transport = VCC::Devices::DriveWireTransport; using namespace std::chrono_literals; static void require(bool ok, const char* reason) { if (!ok) throw std::runtime_error(reason); } template static void waitFor(F test, const char* reason) { auto deadline = std::chrono::steady_clock::now() + 8s; do { if (test()) return; std::this_thread::sleep_for(10ms); } while (std::chrono::steady_clock::now() < deadline); throw std::runtime_error(reason); } static void send(Transport& channel, const std::string& text) { require(channel.write(reinterpret_cast(text.data()), text.size()), "Shell write failed"); } static std::string readThrough(Transport& channel, const std::string& marker) { std::string text; waitFor([&]{ std::uint8_t bytes[1024]; auto count = channel.read(bytes, sizeof bytes); text.append(reinterpret_cast(bytes), count); if (channel.state() == Transport::State::failed) throw std::runtime_error("Shell failed to start"); return text.find(marker) != std::string::npos; }, "Shell output did not match"); return text; } int main() try { DriveWireShell shell(DriveWireShell::StartupFiles::isolatedTest); require(shell.status() == "idle", "Unexpected initial status"); auto denied = shell.connect(); auto id = shell.requestID(); require(id && denied && shell.status() == "requested", "Missing permission request"); require(denied->state() == Transport::State::connecting && !shell.connect(), "Unapproved session is usable"); require(shell.decide(id, false) && denied->state() == Transport::State::failed, "Deny failed"); auto abandoned = shell.connect(); auto abandonedId = shell.requestID(); require(abandonedId != id && !shell.decide(id, true), "Stale decision accepted"); abandoned.reset(); require(shell.status() == "idle" && !shell.decide(abandonedId, true), "Disconnected request was actionable"); auto requested = shell.connect(); require(shell.decide(shell.requestID(), true), "Approval failed"); require(!shell.decide(abandonedId, true), "Old request revived"); waitFor([&]{return requested->state() == Transport::State::connected;}, "Approved shell failed to start"); send(*requested, "test -t 0 && test -t 1 && printf 'shell-pid=%d; cwd-test=%s; term=%s; tty=%s\\n' \"$$\" \"$(test \"$PWD\" = \"$HOME\" && echo yes)\" \"$TERM\" \"yes\"\r"); auto output = readThrough(*requested, "; cwd-test=yes; term=vt100; tty=yes"); auto pos = output.rfind("shell-pid=", output.rfind("; cwd-test=yes")); require(pos != std::string::npos, "Missing shell pid"); pid_t child = std::stoi(output.substr(pos + 10)); require(child > 1, "Invalid child pid"); send(*requested,"printf 'terminal-%s\\n' \"$(tty write(reinterpret_cast(excessive.data()), excessive.size()), "Oversized guest input was accepted"); send(*requested, "sh -c 'trap \"\" HUP; printf \"foreground-%d-active\\n\" \"$$\"; exec sleep 30'\r"); std::string foregroundOutput; pid_t foreground = -1; waitFor([&]{ std::uint8_t bytes[1024]; foregroundOutput.append(reinterpret_cast(bytes), requested->read(bytes, sizeof bytes)); auto offset = foregroundOutput.rfind("foreground-"); if (offset == std::string::npos || offset + 11 >= foregroundOutput.size()) return false; char* end = nullptr; auto candidate = std::strtol(foregroundOutput.c_str() + offset + 11, &end, 10); if (candidate <= 1 || std::string(end).rfind("-active", 0) != 0) return false; foreground = static_cast(candidate); return true; }, "Foreground job failed to start"); shell.stop(); require(shell.status() == "idle", "Stop did not revoke permission"); waitFor([&]{return kill(child, 0) == -1 && errno == ESRCH;}, "Stopped shell was not reaped"); waitFor([&]{return kill(foreground, 0) == -1 && errno == ESRCH;}, "Foreground job survived an explicit stop"); shell.arm(); require(shell.status() == "ready" && shell.requestID() == 0, "Manual start did not arm"); auto explicitSession = shell.connect(); waitFor([&]{return explicitSession->state() == Transport::State::connected;}, "Manual session failed to start"); require(shell.status() == "connected" && !shell.connect(), "Multiple active sessions accepted"); send(*explicitSession, "printf 'end-%s\\n' marker; exit\r"); readThrough(*explicitSession, "end-marker"); waitFor([&]{return explicitSession->state() == Transport::State::closed;}, "Logout did not close the PTY"); require(shell.status() == "idle", "Shell did not release the session"); auto later = shell.connect(); require(later->state() == Transport::State::connecting && shell.status() == "requested", "Invitation reused"); shell.stop(); require(later->state() == Transport::State::failed, "Cancelled request did not fail"); std::cout << "DriveWire shell permission lifetime, PTY, stop, logout and reaping passed.\n"; return 0; } catch (const std::exception& error) { std::cerr << error.what() << '\n'; return 1; }