|
| 1 | +"""Robot preparation preserves pinned geometry, attribution and offline operation.""" |
| 2 | + |
| 3 | +import hashlib |
| 4 | +import io |
| 5 | +import urllib.request |
| 6 | +from pathlib import Path |
| 7 | +from zipfile import ZIP_DEFLATED, ZipFile |
| 8 | + |
| 9 | +import pytest |
| 10 | +from rua_dev.robot_assets import prepare_robot_assets |
| 11 | + |
| 12 | +ASSET_CONTENTS = { |
| 13 | + "robot.urdf": b'<robot name="fixture"><link name="base"/></robot>\n', |
| 14 | + "base.stl": b"fixture mesh geometry\n", |
| 15 | + "LICENSE.txt": b"fixture model license\n", |
| 16 | + "README.md": b"fixture model attribution\n", |
| 17 | +} |
| 18 | + |
| 19 | + |
| 20 | +def create_asset_source(repository: Path) -> tuple[Path, bytes]: |
| 21 | + (repository / "robots").mkdir() |
| 22 | + archive_buffer = io.BytesIO() |
| 23 | + with ZipFile(archive_buffer, "w", compression=ZIP_DEFLATED) as archive: |
| 24 | + for name, content in ASSET_CONTENTS.items(): |
| 25 | + archive.writestr(f"model/{name}", content) |
| 26 | + archive.writestr("unrelated/private.txt", b"not a robot asset") |
| 27 | + archive_content = archive_buffer.getvalue() |
| 28 | + archive_digest = hashlib.sha256(archive_content).hexdigest() |
| 29 | + manifest = repository / "robots/so101-assets.toml" |
| 30 | + manifest.write_text( |
| 31 | + 'archive_url = "https://offline.invalid/so101.whl"\n' |
| 32 | + f'archive_sha256 = "{archive_digest}"\n' |
| 33 | + 'archive_prefix = "model/"\n\n[files]\n' |
| 34 | + + "".join( |
| 35 | + f'"{name}" = "{hashlib.sha256(content).hexdigest()}"\n' |
| 36 | + for name, content in ASSET_CONTENTS.items() |
| 37 | + ) |
| 38 | + ) |
| 39 | + cache = repository / ".cache/so101" |
| 40 | + cache.mkdir(parents=True) |
| 41 | + (cache / f"{archive_digest}.whl").write_bytes(archive_content) |
| 42 | + return manifest, archive_content |
| 43 | + |
| 44 | + |
| 45 | +def test_cached_release_prepares_exact_assets_and_repairs_tampering_offline( |
| 46 | + tmp_path: Path, |
| 47 | +) -> None: |
| 48 | + create_asset_source(tmp_path) |
| 49 | + assets = prepare_robot_assets(tmp_path) |
| 50 | + assert {asset.name: asset.read_bytes() for asset in assets} == ASSET_CONTENTS |
| 51 | + assert not (tmp_path / "robots/so101/private.txt").exists() |
| 52 | + (tmp_path / "robots/so101/base.stl").write_bytes(b"unreviewed local edit") |
| 53 | + prepare_robot_assets(tmp_path) |
| 54 | + assert ( |
| 55 | + tmp_path / "robots/so101/base.stl" |
| 56 | + ).read_bytes() == b"fixture mesh geometry\n" |
| 57 | + # Once prepared, the runtime files alone are sufficient, even without a cache. |
| 58 | + for cached_archive in (tmp_path / ".cache/so101").iterdir(): |
| 59 | + cached_archive.unlink() |
| 60 | + assert { |
| 61 | + asset.name: asset.read_bytes() for asset in prepare_robot_assets(tmp_path) |
| 62 | + } == ASSET_CONTENTS |
| 63 | + |
| 64 | + |
| 65 | +def test_untrusted_download_does_not_replace_existing_geometry( |
| 66 | + tmp_path: Path, monkeypatch: pytest.MonkeyPatch |
| 67 | +) -> None: |
| 68 | + create_asset_source(tmp_path) |
| 69 | + destination = tmp_path / "robots/so101" |
| 70 | + destination.mkdir() |
| 71 | + (destination / "robot.urdf").write_bytes(b"previous geometry") |
| 72 | + for cached_archive in (tmp_path / ".cache/so101").iterdir(): |
| 73 | + cached_archive.unlink() |
| 74 | + |
| 75 | + def download_archive(url: str, timeout: int) -> io.BytesIO: |
| 76 | + return io.BytesIO(b"corrupt download") |
| 77 | + |
| 78 | + monkeypatch.setattr(urllib.request, "urlopen", download_archive) |
| 79 | + with pytest.raises(ValueError, match="archive checksum"): |
| 80 | + prepare_robot_assets(tmp_path) |
| 81 | + assert (destination / "robot.urdf").read_bytes() == b"previous geometry" |
| 82 | + assert sorted(path.name for path in destination.iterdir()) == ["robot.urdf"] |
| 83 | + |
| 84 | + |
| 85 | +def test_mismatched_asset_pin_rejects_the_entire_update(tmp_path: Path) -> None: |
| 86 | + manifest, _ = create_asset_source(tmp_path) |
| 87 | + destination = tmp_path / "robots/so101" |
| 88 | + destination.mkdir() |
| 89 | + (destination / "robot.urdf").write_bytes(b"previous geometry") |
| 90 | + manifest.write_text( |
| 91 | + manifest.read_text().replace( |
| 92 | + hashlib.sha256(ASSET_CONTENTS["LICENSE.txt"]).hexdigest(), "0" * 64 |
| 93 | + ) |
| 94 | + ) |
| 95 | + with pytest.raises(ValueError, match="asset checksum"): |
| 96 | + prepare_robot_assets(tmp_path) |
| 97 | + assert (destination / "robot.urdf").read_bytes() == b"previous geometry" |
| 98 | + assert sorted(path.name for path in destination.iterdir()) == ["robot.urdf"] |
| 99 | + |
| 100 | + |
| 101 | +@pytest.mark.parametrize( |
| 102 | + "filename", ["../outside.stl", "/outside.stl", "..\\outside.stl"] |
| 103 | +) |
| 104 | +def test_asset_pin_cannot_write_outside_its_generated_directory( |
| 105 | + tmp_path: Path, filename: str |
| 106 | +) -> None: |
| 107 | + manifest, _ = create_asset_source(tmp_path) |
| 108 | + # Literal TOML keys preserve backslashes for the traversal case. |
| 109 | + manifest.write_text(manifest.read_text() + f"'{filename}' = '{'0' * 64}'\n") |
| 110 | + with pytest.raises(ValueError, match="filename"): |
| 111 | + prepare_robot_assets(tmp_path) |
| 112 | + assert not (tmp_path / "robots/so101").exists() |
| 113 | + assert not (tmp_path / "robots/outside.stl").exists() |
0 commit comments