"""Regression: slow durable handoff must not extend a native startgrant.""" import io import os from pathlib import Path import sys import tempfile from contextlib import ExitStack from types import SimpleNamespace from unittest import TestCase from unittest.mock import Mock, patch from test_guard import apt, protocol, transaction from test_native_acceptance import context class HookDeadlineTests(TestCase): def test_deadline_after_fsync_vetoes_keeps_marker_and_prevents_reuse(self): # Test both clocks together and each independently: neither clock can # compensate for expiry of the other at the post-fsync gate. for expired in ('both', 'utc', 'monotonic'): with self.subTest(expired=expired), tempfile.TemporaryDirectory(dir='/tmp/opencode') as temp: active = Path(temp) ctx = context('apt-execute') tx = apt.ResolvedAptTransaction.parse(transaction()) clock = {'utc_ns': 2000 * 1000000, 'monotonic_ns': 100} ready = apt.canonical({'transaction_sha256': ctx.transaction_sha256, 'sandbox_version': 1}) original_fsync = os.fsync sync_count = 0 def slow_fsync(fd): nonlocal sync_count original_fsync(fd) sync_count += 1 if sync_count == 2: # directory fsync finished successfully if expired in ('both', 'utc'): clock['utc_ns'] = ctx.start_before_ms * 1000000 if expired in ('both', 'monotonic'): clock['monotonic_ns'] = ctx.monotonic_start_before_ns dpkg_start = Mock() def apt_handoff(): # Models the CLI's Veto -> nonzero contract and APT's # mandatory zero-only gate. No real APT/dpkg is started. try: code = apt.main() except apt.Veto as error: return 100, str(error) if code == 0: dpkg_start() return code, None with ExitStack() as stack: for replacement in ( patch.object(apt, 'ACTIVE', active), patch.object(apt.os, 'geteuid', return_value=0), patch.dict(os.environ, {'APT_HOOK_INFO_FD': '0', 'DPKG_FRONTEND_LOCKED': 'true'}), patch.object(apt, 'load_active', return_value=(tx, ctx, {})), patch.object(apt, 'load_signed_action', return_value={}), patch.object(apt, 'runtime_identity', return_value={}), patch.object(apt, 'check_native_gate'), patch.object(apt, 'inspect_sandbox'), patch.object(apt, 'sealed_read', return_value=ready), patch.object(apt, 'check_preconditions'), patch.object(apt, 'verify_archive'), patch.object(apt, 'secure_path'), patch.object(Path, 'read_text', return_value=ctx.boot_id), patch.object(apt.time, 'time_ns', side_effect=lambda: clock['utc_ns']), patch.object(apt.time, 'monotonic_ns', side_effect=lambda: clock['monotonic_ns']), patch.object(apt.os, 'fsync', side_effect=slow_fsync), patch.object(sys, 'argv', ['apt_transaction.py', 'hook']), patch.object(sys, 'stdin', SimpleNamespace(buffer=io.BytesIO(protocol()))), ): stack.enter_context(replacement) self.assertEqual(apt_handoff(), (100, 'START_DEADLINE')) self.assertEqual(sync_count, 2) marker = active / 'hook-armed.json' self.assertTrue(marker.is_file()) marker_bytes = marker.read_bytes() dpkg_start.assert_not_called() # Even a subsequently valid clock cannot reuse the durable # marker: no reset/delete/retry shortcut exists. clock.update(utc_ns=2000 * 1000000, monotonic_ns=100) with patch.object(sys, 'stdin', SimpleNamespace(buffer=io.BytesIO(protocol()))): self.assertEqual(apt_handoff(), (100, 'ALREADY_HANDED_OFF')) self.assertEqual(marker.read_bytes(), marker_bytes) self.assertEqual(sync_count, 2) dpkg_start.assert_not_called()