Files
OBLITERATUS/tests/test_distributed_source_race.py
T

44 lines
1.6 KiB
Python

"""A read-only observation does not authorize traversal-time inode reuse."""
from pathlib import Path
import pytest
from obliteratus.distributed.contracts import RuntimeContractError
from obliteratus.distributed.preflight import inspect_source
def test_source_inode_moved_between_entries_is_rejected_before_parsing(tmp_path, monkeypatch):
source = tmp_path / "source"
source.mkdir()
first, second = source / "a.json", source / "b.json"
first.write_text("{}")
second.write_text("{}")
for path in (first, second):
path.chmod(0o444)
source.chmod(0o555)
stat = Path.stat
observations = []
def move_after_observation(path, *args, **kwargs):
observed = stat(path, *args, **kwargs)
if path == first:
observations.append(observed)
# The first observation checks symlinks; the second enters the
# file inventory. Move the actual inode before the next entry.
if len(observations) == 2:
source.chmod(0o755)
first.replace(second)
source.chmod(0o555)
return observed
monkeypatch.setattr(Path, "stat", move_after_observation)
try:
with pytest.raises(RuntimeContractError, match="cannot alias the same inode") as failure:
inspect_source(source, max_files=2, max_total_bytes=128, max_file_bytes=64, timeout_seconds=5)
assert failure.value.code == "LMS_SOURCE_BOUNDARY_VIOLATION"
assert observations[-1].st_nlink == second.stat().st_nlink == 1
assert observations[-1].st_ino == second.stat().st_ino
finally:
source.chmod(0o700)