Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions tests/test_manifest_verifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -1084,5 +1084,117 @@ def test_cli_cache_flag(self) -> None:
self.assertNotIn(b"\r\n", f.read())


class TestSaveVerifyCache(unittest.TestCase):
def setUp(self) -> None:
self.tmp = tempfile.TemporaryDirectory()
self.addCleanup(self.tmp.cleanup)
self.cache = os.path.join(self.tmp.name, "cache.json")
self.neighbor = self.cache + ".tmp"
self.previous = b'{"version": 1, "files": {}}\n'
self.unrelated = b"an unrelated file that must survive"
with open(self.cache, "wb") as f:
f.write(self.previous)
with open(self.neighbor, "wb") as f:
f.write(self.unrelated)
self.entries = {"synthetic": {"stamp": [1, 2, 3, 4, 5], "sha256": "a" * 64}}

def assert_files_preserved(self) -> None:
with open(self.cache, "rb") as f:
self.assertEqual(f.read(), self.previous)
with open(self.neighbor, "rb") as f:
self.assertEqual(f.read(), self.unrelated)
self.assertEqual(sorted(os.listdir(self.tmp.name)), ["cache.json", "cache.json.tmp"])

def test_save_preserves_existing_tmp_neighbor(self) -> None:
mv.save_cache(self.cache, self.entries)
self.assertEqual(mv.load_cache(self.cache), self.entries)
with open(self.neighbor, "rb") as f:
self.assertEqual(f.read(), self.unrelated)
self.assertEqual(sorted(os.listdir(self.tmp.name)), ["cache.json", "cache.json.tmp"])

def test_serialization_failure_preserves_cache_and_neighbor(self) -> None:
with self.assertRaises(TypeError):
mv.save_cache(self.cache, {"not_json": object()})
self.assert_files_preserved()

def test_replace_failure_preserves_cache_and_neighbor(self) -> None:
with patch.object(mv.os, "replace", side_effect=PermissionError("replace denied")):
with self.assertRaisesRegex(PermissionError, "replace denied"):
mv.save_cache(self.cache, self.entries)
self.assert_files_preserved()

def test_overlapping_saves_have_independent_staging_files(self) -> None:
real_replace = os.replace
staged = []
inner = {"other": {"stamp": [5, 4, 3, 2, 1], "sha256": "b" * 64}}

def interleave(source, destination):
staged.append(source)
if len(staged) == 1:
# Finish a second save while the first is staged but not published.
mv.save_cache(self.cache, inner)
self.assertEqual(mv.load_cache(self.cache), inner)
real_replace(source, destination)

with patch.object(mv.os, "replace", side_effect=interleave):
mv.save_cache(self.cache, self.entries)
self.assertEqual(len(set(staged)), 2)
self.assertEqual(mv.load_cache(self.cache), self.entries)
self.assertEqual(sorted(os.listdir(self.tmp.name)), ["cache.json", "cache.json.tmp"])

def test_manifest_at_old_staging_name_survives_verification(self) -> None:
game = os.path.join(self.tmp.name, "game")
os.mkdir(game)
payload = b"synthetic data"
path = os.path.join(game, "sample.bin")
with open(path, "wb") as f:
f.write(payload)
os.utime(path, (time.time() - 60,) * 2)
manifest = json.dumps({"files": {"sample.bin": {
"size": len(payload), "sha256": hashlib.sha256(payload).hexdigest(),
}}}).encode("utf-8")
with open(self.neighbor, "wb") as f:
f.write(manifest)
report = mv.verify_manifest(game, self.neighbor, cache_path=self.cache)
self.assertTrue(report.passed)
with open(self.neighbor, "rb") as f:
self.assertEqual(f.read(), manifest)
self.assertEqual(len(mv.load_cache(self.cache)), 1)

def test_posix_mode_is_applied_before_publication(self) -> None:
real_replace = os.replace
def publish(source, destination):
mode.assert_called_once_with(self.cache)
chmod.assert_called_once_with(source, 0o640)
real_replace(source, destination)

with patch.object(mv.os, "name", "posix"), \
patch.object(mv, "published_mode", return_value=0o640, create=True) as mode, \
patch.object(mv.os, "chmod") as chmod, \
patch.object(mv.os, "replace", side_effect=publish):
mv.save_cache(self.cache, self.entries)
self.assertEqual(mv.load_cache(self.cache), self.entries)

def test_mode_error_preserves_cache_and_removes_staging(self) -> None:
for operation in ("mode", "chmod"):
with self.subTest(operation=operation):
with patch.object(mv.os, "name", "posix"), \
patch.object(mv, "published_mode", return_value=0o640, create=True) as mode, \
patch.object(mv.os, "chmod") as chmod:
(mode if operation == "mode" else chmod).side_effect = OSError("mode failed")
with self.assertRaisesRegex(OSError, "mode failed"):
mv.save_cache(self.cache, self.entries)
self.assert_files_preserved()

@unittest.skipUnless(os.name == "posix", "POSIX permission bits")
def test_permissions_of_existing_and_new_caches(self) -> None:
os.chmod(self.cache, 0o640)
mv.save_cache(self.cache, self.entries)
self.assertEqual(os.stat(self.cache).st_mode & 0o777, 0o640)
os.unlink(self.cache)
mv.save_cache(self.cache, self.entries)
self.assertEqual(os.stat(self.cache).st_mode & 0o777, os.stat(self.neighbor).st_mode & 0o777)


if __name__ == "__main__":
unittest.main()
21 changes: 15 additions & 6 deletions tools/manifest_verifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@
import re
import stat
import sys
import tempfile
import time
from typing import Callable, Optional, TextIO

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from file_mode import published_mode # noqa: E402
from report_paths import same_report_path # noqa: E402

CHUNK = 8 * 1024 * 1024
Expand Down Expand Up @@ -149,16 +151,23 @@ def save_cache(path: str, entries: dict[str, dict]) -> None:
directory = os.path.dirname(os.path.abspath(path))
if directory:
os.makedirs(directory, exist_ok=True)
tmp = path + ".tmp"
tmp = None
try:
with open(tmp, "w", encoding="utf-8", newline="\n") as f:
with tempfile.NamedTemporaryFile(
mode="w", encoding="utf-8", newline="\n", dir=directory,
prefix=".verify-cache-", suffix=".tmp", delete=False,
) as f:
tmp = f.name
json.dump({"version": CACHE_VERSION, "files": dict(sorted(entries.items()))}, f, indent=1)
if os.name != "nt":
os.chmod(tmp, published_mode(path)) # do not publish NamedTemporaryFile's private 0600 mode
os.replace(tmp, path)
except BaseException:
try:
os.unlink(tmp)
except OSError:
pass
if tmp is not None:
try:
os.unlink(tmp)
except OSError:
pass
raise


Expand Down
Loading