Files

205 lines
8.7 KiB
C++

// SPDX-License-Identifier: GPL-3.0-or-later
// Optional real-ROM validation. Inputs remain read-only and HD0 writes are in memory.
#include "MachineSession.h"
#include "DriveWireEndpoint.h"
#include "../devices/DriveWireTransport.h"
#include "../devices/FD502/Fd502Device.h"
#include "../devices/HardDisk/HardDiskDevice.h"
#include <chrono>
#include <cstdint>
#include <fstream>
#include <iostream>
#include <iterator>
#include <memory>
#include <stdexcept>
#include <string>
#include <thread>
#include <vector>
using VCC::Mac::MachineSession;
namespace {
std::vector<std::uint8_t> load(const std::string& path) {
std::ifstream file(path, std::ios::binary);
if (!file) throw std::runtime_error("Could not open " + path);
return {std::istreambuf_iterator<char>(file), {}};
}
std::uint64_t key(char character) {
unsigned position;
if (character >= 'a' && character <= 'z') position = character - 'a' + 1;
else if (character >= '0' && character <= '9') position = character - '0' + 32;
else if (character >= 'A' && character <= 'Z')
return (std::uint64_t{1} << 55) | (std::uint64_t{1} << (character - 'A' + 1));
else if (character == '_') return (std::uint64_t{1} << 52) | (std::uint64_t{1} << 45);
else if (character == '-') position = 45;
else if (character == ':') position = 42;
else if (character == '\x1b') position = 50; // Mac Escape selects CoCo Break.
else if (character == '/') position = 47;
else if (character == '.') position = 46;
else if (character == ' ') position = 31;
else if (character == '\r') position = 48;
else throw std::runtime_error("Unsupported key in test input");
return std::uint64_t{1} << position;
}
void frames(MachineSession& machine, unsigned count) {
while (count--) machine.advance();
}
void press(MachineSession& machine, std::uint64_t keys) {
machine.setKeyboard(keys);
frames(machine, 4);
machine.setKeyboard(0);
frames(machine, 4);
}
void typeText(MachineSession& machine, const std::string& text) {
for (char c : text) press(machine, key(c));
}
void typeLine(MachineSession& machine, const std::string& line) {
typeText(machine, line + "\r");
}
void waitFor(MachineSession& machine, const std::string& expected) {
using namespace std::chrono;
const auto deadline = steady_clock::now() + seconds(20);
std::string text;
do {
frames(machine, 20);
try { text = machine.copyScreenText(); } catch (const std::logic_error&) { /* Graphics during boot. */ }
if (text.find(expected) != std::string::npos) return;
std::this_thread::sleep_for(milliseconds(1));
} while (steady_clock::now() < deadline);
throw std::runtime_error("Timed out waiting for " + expected + "\nGuest display:\n" + text);
}
void checkVi(MachineSession& machine, const std::string& path) {
// This name is typed into the shell. Accept only the runner's private,
// simple path so this test can never interpolate shell punctuation.
if (path.rfind("/tmp/vccvi", 0) != 0 ||
path.find_first_not_of("abcdefghijklmnopqrstuvwxyz0123456789/_") != std::string::npos ||
path.size() > 55) throw std::invalid_argument("Unexpected vi test path");
typeLine(machine, "/usr/bin/vi -u NONE -i NONE -n " + path);
waitFor(machine, "line01");
typeText(machine, "G");
waitFor(machine, "line40");
if (machine.copyScreenText().find("line01") != std::string::npos)
throw std::runtime_error("vi did not scroll the first line off the CoCo screen");
typeText(machine, "dd");
typeText(machine, "kj0llhrx"); // vi cursor keys; change the i in line39 to x.
waitFor(machine, "lxne39");
// Option-Arrow on Mac uses the CoCo ALT key and a cursor-key position.
const auto option = std::uint64_t{1} << 51;
auto optionArrow = [&](unsigned position) {
press(machine, option | (std::uint64_t{1} << position));
};
optionArrow(27); // Up one line, retaining the column.
typeText(machine, "rU");
optionArrow(30); // Right
typeText(machine, "rR");
optionArrow(29); // Left
typeText(machine, "rL");
optionArrow(28); // Down
typeText(machine, "rD");
typeText(machine, "otypedfromcoco\rfinal");
waitFor(machine, "typedfromcoco\nfinal");
std::cout << "vi edit on the CoCo screen:\n" << machine.copyScreenText() << std::flush;
typeText(machine, "\x1b"); // the physical Mac Escape key via the CoCo keyboard
typeLine(machine, ":wq");
typeLine(machine, "echo editorsaved");
waitFor(machine, "\neditorsaved\n");
typeLine(machine, "clear");
const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(15);
std::string screen;
do {
frames(machine, 20);
screen = machine.copyScreenText();
const auto first = screen.find_first_not_of(" \n");
if (first != std::string::npos && screen.substr(first) == "$\n") {
std::cout << "After clear: " << screen.substr(first) << std::flush;
return;
}
std::this_thread::sleep_for(std::chrono::milliseconds(2));
} while (std::chrono::steady_clock::now() < deadline);
throw std::runtime_error("Expected only the shell prompt after clear:\n" + screen);
}
void check(const std::string& local, const std::string& port, VCC::Mac::Processor processor,
const std::string& viPath) {
using namespace VCC::Devices;
// A real shell, with startup scripts and history isolated from the developer.
VCC::Mac::DriveWireShell shell(VCC::Mac::DriveWireShell::StartupFiles::isolatedTest);
const DriveWireFactory endpoint = [&shell](const std::string& host, std::uint16_t port) {
return VCC::Mac::openDriveWireEndpoint(shell, host, port);
};
MachineSession machine;
auto hard = std::make_shared<HardDiskDevice>();
hard->mount(0, HardDiskImage(load(local + "/Media/NitrOS-9 L2 V3.3.0.vhd")));
auto floppy = std::make_shared<Fd502Device>(load(local + "/ROMs/rgbdos.rom"));
machine.cartridges().configure({nullptr, hard, nullptr, nullptr, floppy}, true, 4);
machine.load(load(local + "/ROMs/coco3.rom"), processor);
const bool enabledAtStartup = processor == VCC::Mac::Processor::HD6309;
if (enabledAtStartup) machine.setDriveWireFactory(endpoint);
machine.requestNitros9Boot();
machine.resume();
waitFor(machine, "{Term|02}/DD:");
if (machine.copyScreenText().find("ERROR #") != std::string::npos)
throw std::runtime_error("OS-9 startup reported an error: " + machine.copyScreenText());
if (!enabledAtStartup) machine.setDriveWireFactory(endpoint);
for (auto session : {"1", "2"}) {
const std::string suffix(session);
// Give each connection its own screen so the previous prompt and
// disconnect message cannot satisfy the next session's assertions.
typeLine(machine, "display 0c");
frames(machine, 100);
typeLine(machine, "telnet 127.0.0.1 " + port);
waitFor(machine, "Escape character");
typeLine(machine, "localpayload" + suffix);
waitFor(machine, "remoteack" + suffix);
typeLine(machine, "goodbye" + suffix);
waitFor(machine, "Connection closed by foreign host.");
frames(machine, 100);
}
typeLine(machine, "display 0c");
frames(machine, 100);
const bool startFromMac = processor == VCC::Mac::Processor::MC6809;
if (startFromMac) shell.arm();
typeLine(machine, "telnet macshell 2323");
if (!startFromMac) {
const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(10);
while (!shell.requestID() && std::chrono::steady_clock::now() < deadline) frames(machine, 20);
if (!shell.decide(shell.requestID(), true))
throw std::runtime_error("No guest-initiated Mac shell request to approve");
}
waitFor(machine, "Escape character");
typeLine(machine, "/usr/bin/uname");
waitFor(machine, "Darwin");
checkVi(machine, viPath);
typeLine(machine, "exit");
waitFor(machine, "Connection closed by foreign host.\n{Term|02}/DD:");
if (shell.status() != "idle")
throw std::runtime_error("Mac shell invitation was still active after exit");
}
}
int main(int argc, char** argv) try {
if (argc != 5 || (std::string(argv[3]) != "6809" && std::string(argv[3]) != "6309")) {
std::cerr << "Usage: drivewire-guest LOCAL_RESOURCES ECHO_PORT 6809|6309 TEMPORARY_VI_FILE\n";
return 2;
}
const auto processor = std::string(argv[3]) == "6809" ? VCC::Mac::Processor::MC6809 : VCC::Mac::Processor::HD6309;
check(argv[1], argv[2], processor, argv[4]);
std::cout << "PASS: stock NitrOS-9 telnet exchanged data, reconnected and edited and saved in vi on the Mac shell on " << argv[3] << '\n';
return 0;
} catch (const std::exception& error) {
std::cerr << error.what() << '\n';
return 1;
}