Files

93 lines
4.2 KiB
Python

import subprocess
import pytest
def test_wmi_timeout_is_unknown_not_idle(monkeypatch):
from cbb_refresh_verify.owner_probe import query_owners
def hang(command, **kw):
assert 0 < kw['timeout'] <= 10
raise subprocess.TimeoutExpired(command, kw['timeout'])
monkeypatch.setattr(subprocess, 'run', hang)
with pytest.raises(RuntimeError, match='OWNER_QUERY_TIMEOUT'):
query_owners(['ExpeditionPCB.exe'])
def test_malformed_wmi_response_is_not_empty_owner_list(monkeypatch):
from cbb_refresh_verify.owner_probe import query_owners
from pathlib import Path
def run(command, **kw):
Path(command[-1]).with_name('result.json').write_text('{}')
return subprocess.CompletedProcess(command, 0)
monkeypatch.setattr(subprocess, 'run', run)
with pytest.raises(RuntimeError, match='OWNER_QUERY_UNCONFIRMED'):
query_owners(['ExpeditionPCB.exe'])
def test_full_snapshot_timeout_is_bounded_and_not_idle(monkeypatch):
from cbb_refresh_verify.owner_probe import query_process_snapshot
import json
from pathlib import Path
def hang(command, **kw):
assert json.loads(Path(command[-1]).read_text('utf-8')) == {'snapshot_version': 1}
assert 0 < kw['timeout'] <= .5
raise subprocess.TimeoutExpired(command, kw['timeout'])
monkeypatch.setattr(subprocess, 'run', hang)
with pytest.raises(RuntimeError, match='OWNER_QUERY_TIMEOUT'):
query_process_snapshot(timeout_seconds=.5)
def test_full_snapshot_keeps_unknown_processes_and_creation_identity(monkeypatch):
from cbb_refresh_verify.owner_probe import query_process_snapshot
import json
from pathlib import Path
rows = [{'pid': 0, 'parent': 0, 'name': 'System Idle Process',
'command': '', 'executable': '', 'created': ''},
{'pid': 999, 'parent': 100, 'name': 'unlisted-child.exe',
'command': 'unlisted-child.exe', 'executable': 'C:/native/unlisted-child.exe',
'created': '20261105123456.000000+480'}]
def run(command, **kw):
Path(command[-1]).with_name('result.json').write_text(json.dumps({
'snapshot_version': 1, 'owners': rows}), encoding='utf-8')
return subprocess.CompletedProcess(command, 0)
monkeypatch.setattr(subprocess, 'run', run)
assert query_process_snapshot() == rows
@pytest.mark.parametrize('change', ['version', 'created', 'duplicate', 'pid'])
def test_full_snapshot_rejects_incomplete_response(monkeypatch, change):
from cbb_refresh_verify.owner_probe import query_process_snapshot
import json
from pathlib import Path
row = {'pid': 10, 'parent': 1, 'name': 'child.exe', 'command': '',
'executable': '', 'created': ''}
record = {'snapshot_version': 1, 'owners': [row]}
if change == 'version': record['snapshot_version'] = 2
if change == 'created': del row['created']
if change == 'duplicate': record['owners'].append(dict(row))
if change == 'pid': row['pid'] = -1
def run(command, **kw):
Path(command[-1]).with_name('result.json').write_text(json.dumps(record), encoding='utf-8')
return subprocess.CompletedProcess(command, 0)
monkeypatch.setattr(subprocess, 'run', run)
with pytest.raises(RuntimeError, match='OWNER_QUERY_UNCONFIRMED'):
query_process_snapshot()
def test_full_snapshot_helper_does_not_filter_unknown_descendants(tmp_path, monkeypatch):
from cbb_refresh_verify.owner_probe import owner_probe_main
import win32com.client
from types import SimpleNamespace
import json
queries = []
def query(sql):
queries.append(sql)
return [SimpleNamespace(ProcessId=42, ParentProcessId=12, Name='custom-native-child.exe',
CommandLine='custom-native-child.exe', ExecutablePath='C:/custom-native-child.exe',
CreationDate='creation-time')]
monkeypatch.setattr(win32com.client, 'GetObject', lambda _: SimpleNamespace(ExecQuery=query))
job = tmp_path/'request.json'; job.write_text('{"snapshot_version": 1}')
assert owner_probe_main(job) == 0
assert len(queries) == 1 and 'WHERE' not in queries[0]
row = json.loads(job.with_name('result.json').read_text('utf-8'))['owners'][0]
assert row['name'] == 'custom-native-child.exe' and row['created'] == 'creation-time'