91 lines
6.7 KiB
Java
91 lines
6.7 KiB
Java
package p.studio.app.runtime;
|
|
|
|
import com.fasterxml.jackson.databind.*;
|
|
import java.io.*;
|
|
import java.math.BigInteger;
|
|
import java.nio.file.*;
|
|
import java.util.*;
|
|
|
|
/** Deliberately adversarial child process. Never packaged into production. */
|
|
public final class HeadlessFixture {
|
|
static final ObjectMapper MAPPER = new ObjectMapper();
|
|
// Successful JVM/ImageIO startup and fragmented IO need headroom on CI. Only
|
|
// fixtures that deliberately hang use short call or shutdown deadlines.
|
|
static HeadlessLimits limits(String mode) {
|
|
boolean hungCall = mode.equals("hang-call") || mode.equals("hang-cancel");
|
|
boolean hungShutdown = hungCall || mode.equals("descendant");
|
|
return new HeadlessLimits(600, 4096, 4096, 5000,
|
|
hungCall ? 500 : 5000, hungShutdown ? 150 : 2000);
|
|
}
|
|
static String mode;
|
|
static void emit(JsonNode value) throws Exception {
|
|
byte[] bytes = (value.toString() + "\r\n").getBytes(java.nio.charset.StandardCharsets.UTF_8);
|
|
for (int at = 0; at < bytes.length; at += 7) { System.out.write(bytes, at, Math.min(7, bytes.length - at)); System.out.flush(); }
|
|
}
|
|
static JsonNode state() { return MAPPER.createObjectNode().put("pc", 0).put("logicalFrameIndex", new BigInteger("18446744073709551615"))
|
|
.put("tickIndex", new BigInteger("18446744073709551615")).put("appId", 1).put("status", "paused").set("stackTop", MAPPER.createArrayNode()); }
|
|
static void response(String id, JsonNode result) throws Exception {
|
|
emit(MAPPER.createObjectNode().put("type", "response").put("sessionId", mode.equals("wrong-session") ? "alien" : "fixture").put("requestId", id).set("result", result));
|
|
}
|
|
public static void main(String[] args) throws Exception {
|
|
mode = args[0];
|
|
if (mode.equals("child") || mode.equals("no-hello")) { Thread.sleep(60000); return; }
|
|
if (mode.equals("malformed")) { System.out.println("not json"); Thread.sleep(60000); return; }
|
|
if (mode.equals("oversized")) { System.out.print("x".repeat(5000)); System.out.flush(); Thread.sleep(60000); return; }
|
|
if (mode.equals("utf8")) { System.out.write(new byte[]{(byte)0xff, 10}); System.out.flush(); Thread.sleep(60000); return; }
|
|
var hello = MAPPER.createObjectNode().put("type", "hello").put("sessionId", "fixture").put("runtimeVersion", "fixture-only");
|
|
hello.putObject("protocolVersion").put("major", mode.equals("major") ? 2 : 1).put("minor", 3);
|
|
hello.set("capabilities", MAPPER.valueToTree(mode.equals("capabilities") ? Set.of("boot") : HeadlessWire.CAPABILITIES));
|
|
hello.set("limits", MAPPER.valueToTree(limits(mode)));
|
|
if (mode.equals("limits")) ((com.fasterxml.jackson.databind.node.ObjectNode) hello.path("limits")).put("maxFrames", 900);
|
|
emit(hello);
|
|
if (mode.equals("descendant")) {
|
|
var child = new ProcessBuilder(java(), "-cp", System.getProperty("java.class.path"), HeadlessFixture.class.getName(), "child").start();
|
|
Files.writeString(Path.of("descendant.pid"), Long.toString(child.pid()));
|
|
}
|
|
System.err.println("fixture stderr is not JSONL");
|
|
String active = null; long frames = 0;
|
|
var reader = new BufferedReader(new InputStreamReader(System.in));
|
|
for (String line; (line = reader.readLine()) != null;) {
|
|
var request = MAPPER.readTree(line); String id = request.path("requestId").asText(), command = request.path("command").asText();
|
|
switch (command) {
|
|
case "initialize" -> response(id, MAPPER.createObjectNode().setAll(Map.of("protocolVersion", hello.path("protocolVersion"), "capabilities", hello.path("capabilities"))));
|
|
case "boot", "reset" -> {
|
|
if (mode.startsWith("load-fault-")) {
|
|
var resource = "/runtime/fault-" + mode.substring("load-fault-".length()) + ".json";
|
|
var fault = MAPPER.readTree(HeadlessFixture.class.getResourceAsStream(resource));
|
|
var error = MAPPER.createObjectNode().put("code", "boot_failed").put("message", "Runtime rejected cartridge");
|
|
error.putObject("details").set("fault", fault);
|
|
emit(MAPPER.createObjectNode().put("type", "response").put("sessionId", "fixture").put("requestId", id).set("error", error));
|
|
} else response(id, state());
|
|
}
|
|
case "get_state" -> { if (!mode.equals("hang-call")) response(id, state()); }
|
|
case "get_fault" -> response(id, MAPPER.nullNode());
|
|
case "advance" -> {
|
|
active = id; frames = request.path("params").path("frames").longValue();
|
|
emit(MAPPER.createObjectNode().put("type", "progress").put("sessionId", "fixture").put("requestId", id)
|
|
.put("requested", frames).put("completed", mode.equals("bad-progress") ? frames + 1 : 2));
|
|
if (mode.equals("crash")) System.exit(23);
|
|
if (mode.equals("hang-cancel") || mode.equals("cooperative") || mode.equals("descendant")) continue;
|
|
response(id, MAPPER.createObjectNode().put("requested", frames).put("completed", frames).put("stop", "completed"));
|
|
}
|
|
case "cancel" -> {
|
|
if (mode.equals("hang-cancel") || mode.equals("descendant")) continue;
|
|
response(id, MAPPER.createObjectNode().put("requested", true).put("targetRequestId", active));
|
|
response(active, MAPPER.createObjectNode().put("requested", frames).put("completed", 2).put("stop", "canceled"));
|
|
}
|
|
case "capture" -> {
|
|
var image = new java.awt.image.BufferedImage(1, 1, java.awt.image.BufferedImage.TYPE_INT_ARGB);
|
|
image.setRGB(0, 0, 0xff123456); var bytes = new ByteArrayOutputStream(); javax.imageio.ImageIO.write(image, "png", bytes);
|
|
response(id, MAPPER.createObjectNode().put("mimeType", "image/png").put("encoding", "base64").put("pixelFormat", "RGBA8888")
|
|
.put("width", 1).put("height", 1).put("stridePixels", 3).put("logicalFrameIndex", new BigInteger("18446744073709551615"))
|
|
.put("frameId", new BigInteger("9223372036854775808")).put("data", mode.equals("bad-png") ? "broken" : Base64.getEncoder().encodeToString(bytes.toByteArray())));
|
|
}
|
|
case "shutdown" -> { if (mode.equals("descendant")) continue; response(id, MAPPER.createObjectNode().put("closed", true)); return; }
|
|
default -> response(id, MAPPER.createObjectNode());
|
|
}
|
|
}
|
|
}
|
|
static String java() { return Path.of(System.getProperty("java.home"), "bin", "java").toString(); }
|
|
}
|