diff --git a/tests/check_physics_replay.py b/tests/check_physics_replay.py index a46a4c12..df6b662d 100644 --- a/tests/check_physics_replay.py +++ b/tests/check_physics_replay.py @@ -1,4 +1,5 @@ """Exercise actual recording/replay paths, including a live UDP capture.""" +import base64 import copy import json import math @@ -17,14 +18,14 @@ env = dict(os.environ, QT_QPA_PLATFORM="offscreen") -def run(args, accepted=True): +def run(args, accepted=True, step_seconds=1 / 60): result = subprocess.run([binary, *args], env=env, capture_output=True, text=True, timeout=30) if accepted: assert result.returncode == 0, result.stderr assert "ODE Message" not in result.stderr and "ODE Error" not in result.stderr, result.stderr value = json.loads(result.stdout) assert value["success"] and value["substeps"] == 5 - assert abs(value["step_seconds"] - 1 / 60) < 1e-12 + assert abs(value["step_seconds"] - step_seconds) < 1e-12 assert value["max_shell_penetration_m"] <= 0.002, value["max_shell_penetration_m"] return value assert result.returncode != 0, "invalid recording was accepted" @@ -55,6 +56,47 @@ def difference(left, right): "--settings", str(settings)], accepted=False) assert not output.exists(), "invalid settings produced a recording" print("PASS missing/non-file explicit settings are rejected before recording", flush=True) + settings = directory / "settings.xml" + for contents in ["", '', + "", '', + ""]: + settings.write_text(contents) + output = directory / "malformed-settings-output.json" + run(["--record", str(output), "--fixture", "spaced", "--ticks", "1", + "--settings", str(settings)], accepted=False) + assert not output.exists(), "malformed settings produced a recording" + print("PASS malformed/wrong-root explicit settings are rejected before recording", flush=True) + + # A capture shorter than one valid physics tick can finish without stepping. + # Keep the zero-tick case deterministic as well as checking the real timer. + path = directory / "short.json" + settings.write_text( + '\n\n' + '' + '6' + '' + '0.1') + captured = run(["--record", str(path), "--duration-ms", "50", + "--settings", str(settings)], step_seconds=.1) + replayed = run(["--replay", str(path)], step_seconds=.1) + assert difference(captured, replayed) <= 1e-12 + document = json.loads(path.read_text()) + document["ticks"] = 0 + document["events"] = [] + path.write_text(json.dumps(document)) + empty = run(["--replay", str(path)], step_seconds=.1) + assert empty["ticks"] == 0 and empty["contacts"] == 0 + assert empty["checkpoints"] == [document["initial"]] + # A terminal tick-zero event must still be applied even without a step. + teleport = b"\x0a\x07\x0a\x05\x0d" + struct.pack(" #include #include +#include #include #include #include @@ -423,7 +424,7 @@ int main(int argc, char** argv) throw std::runtime_error("Incompatible replay format or ODE version"); seed = integer(recording, "seed", std::numeric_limits::max()); ticks = integer(recording, "ticks", 1000000); - if (!ticks || !recording["events"].isArray() || !recording["initial"].isObject()) + if (!recording["events"].isArray() || !recording["initial"].isObject()) throw std::runtime_error("Missing replay timeline"); settings = loadSettings(recording["settings"].toObject()); } else { @@ -431,6 +432,15 @@ int main(int argc, char** argv) QFile input(settingsFile); if (!QFileInfo(settingsFile).isFile() || !input.open(QIODevice::ReadOnly)) throw std::runtime_error("Requested settings must be an existing readable regular file"); + // VarXML silently keeps defaults when parsing fails. Validate + // explicit input first so a recording cannot use another setup. + QXmlStreamReader xml(&input); + if (!xml.readNextStartElement() || xml.qualifiedName() != QLatin1String("VarXML") || + !xml.namespaceUri().isEmpty()) + throw std::runtime_error("Requested settings must have a VarXML root"); + while (!xml.atEnd()) xml.readNext(); + if (xml.hasError()) + throw std::runtime_error("Requested settings contain invalid XML"); } ConfigWidget config(false, settingsFile); if (settingsFile.isEmpty()) {