238 lines
11 KiB
Python
238 lines
11 KiB
Python
"""A packaged model must run outside its source tree and reject stale evidence."""
|
|
from __future__ import annotations
|
|
|
|
import hashlib
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import subprocess
|
|
import sys
|
|
import zipfile
|
|
|
|
import pytest
|
|
|
|
from mjrl.platform.bundle import build_bundle, open_bundle, validate_bundle
|
|
from mjrl.platform.play import play
|
|
|
|
|
|
def test_manual_does_not_advertise_motion_on_a_zero_range_axis(tmp_path):
|
|
from mjrl.platform.bundle import _manual
|
|
|
|
fixture = Path(__file__).parent / "fixtures/controls/velocity.yaml"
|
|
(tmp_path / "controls.yaml").write_bytes(fixture.read_bytes())
|
|
(tmp_path / "main.json").write_text('{"kind": "rl_onnx"}')
|
|
(tmp_path / "layout.json").write_text(json.dumps({
|
|
"command_ranges": {"twist": {
|
|
"lin_vel_x": [-1, 1], "lin_vel_y": [0, 0], "ang_vel_z": [-1, 1],
|
|
}},
|
|
}))
|
|
manual = _manual(tmp_path, "robot", {"main": {
|
|
"control_mode": "rl_onnx", "contract": "main.json",
|
|
"layout": "layout.json", "controls": "controls.yaml",
|
|
}}, "main")["modes"][0]
|
|
pad = {entry["control"]: entry["does"] for entry in manual["controls"]}
|
|
keys = {entry["key"]: entry["does"] for entry in manual["keys"]}
|
|
assert pad["Lx"] == "No effect: lin_vel_y is fixed at 0."
|
|
assert keys["A"] == keys["D"] == pad["Lx"]
|
|
assert "Drive lin_vel_x" in pad["Ly"]
|
|
assert keys["W"] == "Increase lin_vel_x (m/s)."
|
|
|
|
|
|
@pytest.fixture
|
|
def model(tmp_path):
|
|
source = tmp_path / "source"
|
|
source.mkdir()
|
|
(source / "part.xml").write_text('<mujoco><worldbody><geom name="shape" type="box" size=".1 .1 .1"/></worldbody></mujoco>')
|
|
path = source / "robot.xml"
|
|
path.write_text('<mujoco><include file="part.xml"/></mujoco>')
|
|
return path
|
|
|
|
|
|
def test_viewer_app_runs_without_source_tree_or_checkout(model, tmp_path):
|
|
app = build_bundle(model, tmp_path / "robot", robot_id="robot", modes={"view": {"control_mode": "model_viewer"}})
|
|
model.unlink()
|
|
model.with_name("part.xml").unlink()
|
|
assert play(app, headless=True, steps=3)["simulation_time"] == pytest.approx(.06)
|
|
# -I removes cwd and PYTHONPATH; the bundled implementation is selected from
|
|
# the executable zip, not an editable installation's source modules.
|
|
result = subprocess.run([sys.executable, "-I", str(tmp_path / "robot/runtime/controller.pyz"),
|
|
"--app", str(app), "--headless", "--steps", "3"],
|
|
cwd=tmp_path, env={k: v for k, v in os.environ.items() if k != "PYTHONPATH"},
|
|
capture_output=True, text=True)
|
|
assert result.returncode == 0, result.stderr
|
|
assert json.loads(result.stdout)["steps"] == 3
|
|
with open_bundle(app) as (root, manifest):
|
|
assert manifest["capabilities"] == ["model_viewer"]
|
|
assert manifest["verification"] is None
|
|
assert "include" not in (root / manifest["model"]).read_text()
|
|
|
|
|
|
@pytest.mark.parametrize("change_identifier", [False, True])
|
|
def test_native_translation_cli_repackages_only_text_changes(model, tmp_path, change_identifier):
|
|
root = tmp_path / "robot"
|
|
app = build_bundle(model, root, robot_id="robot", modes={"view": {"control_mode": "model_viewer"}})
|
|
original_archive = app.read_bytes()
|
|
original_manifest = (root / "bundle.json").read_bytes()
|
|
translated = json.loads((root / "manual.en.json").read_text())
|
|
translated["language"] = "zh"
|
|
translated["modes"][0]["summary"] = "查看机器人模型。"
|
|
if change_identifier:
|
|
translated["modes"][0]["mode"] = "other"
|
|
source = tmp_path / "manual.zh.json"
|
|
source.write_text(json.dumps(translated, ensure_ascii=False), encoding="utf-8")
|
|
result = subprocess.run(
|
|
[sys.executable, str(Path(__file__).resolve().parents[1] / "scripts/deploy.py"),
|
|
"--translate", str(root), "--language", "zh", "--manual", str(source)],
|
|
capture_output=True, text=True,
|
|
)
|
|
if change_identifier:
|
|
assert result.returncode != 0
|
|
assert "changes more than the text" in result.stderr
|
|
assert not (root / "manual.zh.json").exists()
|
|
assert (root / "bundle.json").read_bytes() == original_manifest
|
|
assert app.read_bytes() == original_archive
|
|
return
|
|
assert result.returncode == 0, result.stderr
|
|
assert validate_bundle(root)["manuals"] == {"en": "manual.en.json", "zh": "manual.zh.json"}
|
|
with open_bundle(app) as (unpacked, manifest):
|
|
raw = (unpacked / manifest["manuals"]["zh"]).read_bytes()
|
|
assert json.loads(raw) == translated
|
|
assert manifest["files"]["manual.zh.json"] == {
|
|
"bytes": len(raw), "sha256": hashlib.sha256(raw).hexdigest(),
|
|
}
|
|
assert play(app, headless=True, steps=1)["steps"] == 1
|
|
|
|
|
|
def test_portable_runtime_rejects_app_supplied_schema_override(model, tmp_path):
|
|
root = tmp_path / "robot"
|
|
build_bundle(model, root, robot_id="robot", modes={"view": {"control_mode": "model_viewer"}})
|
|
schema_path = root / "app.schema"
|
|
schema = json.loads(schema_path.read_text(encoding="utf-8"))
|
|
schema["$defs"]["robotApp"]["additionalProperties"] = True
|
|
schema_path.write_text(json.dumps(schema), encoding="utf-8")
|
|
manifest_path = root / "bundle.json"
|
|
manifest = json.loads(manifest_path.read_text(encoding="utf-8"))
|
|
manifest["unexpected_metadata"] = True
|
|
schema_bytes = schema_path.read_bytes()
|
|
manifest["files"]["app.schema"] = {
|
|
"bytes": len(schema_bytes),
|
|
"sha256": hashlib.sha256(schema_bytes).hexdigest(),
|
|
}
|
|
manifest_path.write_text(json.dumps(manifest), encoding="utf-8")
|
|
|
|
runtime = root / "runtime/controller.pyz"
|
|
result = subprocess.run(
|
|
[sys.executable, "-I", str(runtime), "--app", str(root), "--headless", "--steps", "1"],
|
|
cwd=tmp_path,
|
|
capture_output=True,
|
|
text=True,
|
|
)
|
|
assert result.returncode != 0
|
|
assert "Additional properties are not allowed ('unexpected_metadata' was unexpected)" in result.stderr
|
|
|
|
|
|
def test_app_bytes_are_reproducible_and_existing_output_is_refused(model, tmp_path):
|
|
first = build_bundle(model, tmp_path / "one", robot_id="robot", modes={"view": {"control_mode": "model_viewer"}})
|
|
second = build_bundle(model, tmp_path / "two", robot_id="robot", modes={"view": {"control_mode": "model_viewer"}})
|
|
assert first.read_bytes() == second.read_bytes()
|
|
with pytest.raises(FileExistsError):
|
|
build_bundle(model, tmp_path / "one", robot_id="robot", modes={"view": {"control_mode": "model_viewer"}})
|
|
|
|
|
|
def test_controlled_app_does_not_use_compilation_as_acceptance(model, tmp_path):
|
|
config = tmp_path / "controller.json"
|
|
config.write_text('{"kind":"joint_sliders"}')
|
|
with pytest.raises(ValueError, match="verification"):
|
|
build_bundle(model, tmp_path / "rejected", robot_id="robot", modes={"move": {"controller": config}})
|
|
assert not (tmp_path / "rejected").exists()
|
|
assert not (tmp_path / "rejected.app").exists()
|
|
|
|
|
|
def test_tampered_dependency_is_refused_before_model_loading(model, tmp_path):
|
|
build_bundle(model, tmp_path / "robot", robot_id="robot", modes={"view": {"control_mode": "model_viewer"}})
|
|
path = tmp_path / "robot/model/robot.xml"
|
|
path.write_text(path.read_text() + "\n<!-- changed after verification -->\n")
|
|
with pytest.raises(ValueError, match="integrity"):
|
|
validate_bundle(tmp_path / "robot")
|
|
|
|
|
|
@pytest.mark.parametrize("name", ["../escape", "/absolute", "C:/windows", "a\\b"])
|
|
def test_archive_paths_cannot_escape(tmp_path, name):
|
|
app = tmp_path / "bad.app"
|
|
with zipfile.ZipFile(app, "w") as archive:
|
|
archive.writestr(name, "bad")
|
|
with pytest.raises(ValueError, match="unsafe"):
|
|
with open_bundle(app):
|
|
pytest.fail("an unsafe archive was opened")
|
|
|
|
|
|
def test_mesh_dependency_is_carried_and_preserves_physics(tmp_path):
|
|
import trimesh
|
|
import mujoco
|
|
import numpy as np
|
|
source = tmp_path / "source"
|
|
source.mkdir()
|
|
trimesh.creation.box(extents=(.2, .1, .1)).export(source / "shape.stl")
|
|
model = source / "robot.xml"
|
|
model.write_text('<mujoco><asset><mesh name="shape" file="shape.stl"/></asset><worldbody><body><joint name="hinge" type="hinge"/><geom type="mesh" mesh="shape"/></body></worldbody></mujoco>')
|
|
before = mujoco.MjModel.from_xml_path(str(model))
|
|
app = build_bundle(model, tmp_path / "robot", robot_id="robot", modes={"view": {"control_mode": "model_viewer"}})
|
|
(source / "shape.stl").unlink()
|
|
with open_bundle(app) as (root, manifest):
|
|
after = mujoco.MjModel.from_xml_path(str(root / manifest["model"]))
|
|
np.testing.assert_allclose(before.body_mass, after.body_mass)
|
|
assert any(name.startswith("model/meshes/") for name in manifest["files"])
|
|
|
|
|
|
def test_concurrent_publication_cannot_nest_or_replace_an_application(model, tmp_path, monkeypatch):
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
import threading
|
|
from mjrl.platform import bundle
|
|
|
|
entered, resume = threading.Event(), threading.Event()
|
|
original = bundle._runtime
|
|
|
|
def paused_runtime(output):
|
|
entered.set()
|
|
assert resume.wait(10)
|
|
original(output)
|
|
|
|
monkeypatch.setattr(bundle, "_runtime", paused_runtime)
|
|
options = dict(robot_id="robot", modes={"view": {"control_mode": "model_viewer"}})
|
|
with ThreadPoolExecutor(max_workers=1) as executor:
|
|
first = executor.submit(build_bundle, model, tmp_path / "same", **options)
|
|
try:
|
|
assert entered.wait(10)
|
|
with pytest.raises(FileExistsError, match="in progress"):
|
|
build_bundle(model, tmp_path / "same", **options)
|
|
finally:
|
|
resume.set()
|
|
archive = first.result(timeout=10)
|
|
with open_bundle(archive) as (_, manifest):
|
|
assert manifest["robot_id"] == "robot"
|
|
assert not (tmp_path / "same/robot").exists()
|
|
assert not (tmp_path / ".same.build-lock").exists()
|
|
|
|
|
|
@pytest.mark.parametrize("name", ["CON.txt", "parts/LPT1", "parts/file. ", "a:b", "bad\x01name"])
|
|
def test_paths_are_portable_to_windows(tmp_path, name):
|
|
from mjrl.platform.bundle import safe_path
|
|
|
|
with pytest.raises(ValueError, match="unsafe"):
|
|
safe_path(tmp_path, name)
|
|
|
|
|
|
def test_asset_license_travels_with_model_and_is_integrity_checked(model, tmp_path):
|
|
license_file = tmp_path / "asset-license.txt"
|
|
license_file.write_text("Fixture attribution and redistribution conditions.\n")
|
|
app = build_bundle(model, tmp_path / "licensed", robot_id="robot",
|
|
modes={"view": {"control_mode": "model_viewer"}}, licenses=[license_file])
|
|
license_file.unlink()
|
|
with open_bundle(app) as (root, manifest):
|
|
assert manifest["licenses"] == ["LICENSE", "licenses/00-asset-license.txt"]
|
|
assert (root / manifest["licenses"][1]).read_text().startswith("Fixture attribution")
|
|
copied = tmp_path / "licensed/licenses/00-asset-license.txt"
|
|
copied.write_text("different conditions")
|
|
with pytest.raises(ValueError, match="integrity"):
|
|
validate_bundle(tmp_path / "licensed")
|