Files
cbb-refresh-verify-795/tests/test_save_helper_identity.py

56 lines
2.2 KiB
Python

import json
import subprocess
import sys
import time
import pytest
from cbb_refresh_verify.desktop_worker import launch_save_helper
def test_helper_gate_only_opens_after_exact_identity_is_durable(tmp_path):
gate, journal, called = tmp_path/'gate', tmp_path/'worker-state.json', tmp_path/'called.json'
script = tmp_path/'helper.py'
script.write_text('import json,time\nfrom pathlib import Path\n'
f'gate=Path({str(gate)!r})\n'
'while not gate.exists(): time.sleep(.02)\n'
f'Path({str(called)!r}).write_text(Path({str(journal)!r}).read_text())\n')
helper = None
try:
helper = launch_save_helper([sys.executable, str(script)], start_gate=gate,
identity_path=journal, operation_id='test-gate')
helper.wait(5)
record = json.loads(called.read_text())
assert record['operation_id'] == 'test-gate'
identity = record['processes'][0]
assert identity['pid'] == helper.pid
assert identity['created_handle'] and identity['owner_sid']
assert identity['image'].casefold() == sys.executable.casefold()
finally:
if helper is not None and helper.poll() is None:
helper.terminate()
helper.wait(5)
def test_identity_storage_failure_does_not_allow_native_call(tmp_path, monkeypatch):
from cbb_refresh_verify import desktop_worker
gate = tmp_path/'gate'
script = tmp_path/'helper.py'
script.write_text('import time;time.sleep(.5)')
created = []
real_popen = subprocess.Popen
def popen(*a, **kw):
process = real_popen(*a, **kw)
created.append(process)
return process
monkeypatch.setattr(desktop_worker.subprocess, 'Popen', popen)
def fail(*a, **kw):
raise OSError('journal unavailable')
monkeypatch.setattr('cbb_refresh_verify.session_record.write_session_record', fail)
with pytest.raises(OSError, match='journal unavailable'):
launch_save_helper([sys.executable, str(script)], start_gate=gate,
identity_path=tmp_path/'worker-state.json', operation_id='test')
assert not gate.exists()
for process in created:
process.wait(5)