Files
Aidem-Media-DLL-Analysis/tests/test_batch.py
T
Patryk GenschandClaude Opus 5.5 b0013c5114 feat(acquire): resumable batch extraction queue and binary inventory
Inventory classifies the DLL repository by SHA; the batch queue runs each
engine build in a fresh PyGhidra process with a timeout, fingerprints the
extractor environment and keeps per-attempt state and logs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-24 18:24:15 +02:00

132 lines
5.3 KiB
Python

import hashlib
import json
import subprocess
import pytest
from ams.acquire import batch
@pytest.fixture
def setup(tmp_path):
dll = tmp_path / 'engine.dll'
dll.write_bytes(b'engine')
sha = hashlib.sha256(dll.read_bytes()).hexdigest()
manifest = tmp_path / 'manifest.json'
manifest.write_text(json.dumps({'schema': 'binary-inventory/1', 'builds': [
{'sha256': sha, 'paths': [str(dll)], 'scope': 'engine', 'family': 'PIKLIB8'}]}))
return dll, sha, manifest, tmp_path / 'out'
def success(source, output, log, timeout, env):
output.write_text(json.dumps({'binary': {'sha256': batch.digest(source)},
'types': [{'name': 'X'}], 'methods': [{'name': 'Y'}]}))
log.write_text('ok')
def test_success_resume_and_input_change(setup):
dll, sha, manifest, out = setup
assert batch.run_queue(manifest, out, worker=success)['succeeded'] == 1
assert batch.run_queue(manifest, out, worker=success)['skipped_success'] == 1
dll.write_bytes(b'changed')
result = batch.run_queue(manifest, out, worker=success)
assert result['failures'][0]['error'] == 'No source path matches manifest SHA'
assert json.loads((out / (sha + '.snapshot.json')).read_text())['binary']['sha256'] == sha
@pytest.mark.parametrize('kind', ['failed', 'timeout', 'empty', 'mismatch', 'missing'])
def test_failure_retry(setup, kind):
dll, sha, manifest, out = setup
def broken(source, output, log, timeout, env):
if kind == 'failed':
raise RuntimeError('crashed')
if kind == 'timeout':
raise subprocess.TimeoutExpired('worker', timeout)
if kind == 'missing':
return
success(source, output, log, timeout, env)
data = json.loads(output.read_text())
if kind == 'empty':
data['methods'] = []
else:
data['binary']['sha256'] = '0' * 64
output.write_text(json.dumps(data))
assert batch.run_queue(manifest, out, worker=broken)['failures']
assert not (out / (sha + '.snapshot.json')).exists()
assert batch.run_queue(manifest, out, worker=success)['skipped_failed'] == 1
assert batch.run_queue(manifest, out, worker=success, retry_failed=True)['succeeded'] == 1
state = json.loads((out / 'states' / (sha + '.json')).read_text())
assert state['attempts'] == 2
def test_fingerprint_change_and_failure_preserves_snapshot(setup, monkeypatch):
dll, sha, manifest, out = setup
batch.run_queue(manifest, out, worker=success)
previous = (out / (sha + '.snapshot.json')).read_bytes()
monkeypatch.setattr(batch, 'fingerprint', lambda env=None: 'changed')
def fail(*args):
raise RuntimeError('no')
assert batch.run_queue(manifest, out, worker=fail)['attempted'] == 1
assert (out / (sha + '.snapshot.json')).read_bytes() == previous
def test_lock_and_scope_limit(setup):
dll, sha, manifest, out = setup
assert batch.run_queue(manifest, out, limit=0, worker=success)['attempted'] == 0
with batch.queue_lock(out):
with pytest.raises(RuntimeError, match='Another'):
batch.run_queue(manifest, out, worker=success)
data = json.loads(manifest.read_text())
data['builds'][0]['scope'] = 'plugin'
manifest.write_text(json.dumps(data))
assert batch.run_queue(manifest, out, worker=success)['skipped_scope'] == 1
def test_timeout_kills_process_group(tmp_path, monkeypatch):
calls = []
class Process:
pid = 123
def wait(self, timeout=None):
calls.append(('wait', timeout))
if timeout is not None:
raise subprocess.TimeoutExpired('test', timeout)
def popen(*args, **kwargs):
assert kwargs['start_new_session']
return Process()
monkeypatch.setattr(batch.subprocess, 'Popen', popen)
monkeypatch.setattr(batch.os, 'killpg', lambda *args: calls.append(('killpg', args)))
with pytest.raises(subprocess.TimeoutExpired):
batch.execute_worker(tmp_path / 'dll', tmp_path / 'out', tmp_path / 'log', 0.1, {})
assert ('killpg', (123, batch.signal.SIGKILL)) in calls
assert calls[-1] == ('wait', None)
def test_snapshot_tamper_forces_refresh(setup):
dll, sha, manifest, out = setup
batch.run_queue(manifest, out, worker=success)
path = out / (sha + '.snapshot.json')
data = json.loads(path.read_text())
data['methods'].append({'name': 'INVENTED'})
path.write_text(json.dumps(data))
assert batch.run_queue(manifest, out, worker=success)['succeeded'] == 1
state = json.loads((out / 'states' / (sha + '.json')).read_text())
assert state['coverage'] == {'types': 1, 'methods': 1, 'dispatch': 0}
assert state['input_name'] == 'engine.dll'
def test_provider_version_fingerprint(tmp_path):
props = tmp_path / 'Ghidra' / 'application.properties'
props.parent.mkdir()
props.write_text('application.version=12.1')
env = {'GHIDRA_INSTALL_DIR': str(tmp_path), 'AMS_USE_PYGHIDRA': '1'}
before = batch.fingerprint(env)
props.write_text('application.version=12.2')
assert batch.fingerprint(env) != before
assert batch.fingerprint({**env, 'AMS_USE_PYGHIDRA': ''}) != batch.fingerprint(env)
def test_limit_reports_pending(setup):
dll, sha, manifest, out = setup
result = batch.run_queue(manifest, out, limit=0, worker=success)
assert result['pending'] == 1