168 lines
4.7 KiB
Python
168 lines
4.7 KiB
Python
import pytest
|
|
|
|
from src import backend_proxy
|
|
from src import engine
|
|
|
|
|
|
def test_native_backend_available_on_linux():
|
|
import src.utils.platform_utils as pu
|
|
|
|
assert backend_proxy.NATIVE_BACKEND_AVAILABLE == pu.is_linux()
|
|
|
|
|
|
def test_initialize_ok_with_native_backend(monkeypatch):
|
|
monkeypatch.setattr(backend_proxy, 'NATIVE_BACKEND_AVAILABLE', True)
|
|
|
|
assert backend_proxy.initialize() == backend_proxy.InitializationResult.Ok
|
|
|
|
|
|
def test_initialize_without_backend_returns_not_installed(monkeypatch):
|
|
if backend_proxy.BACKEND_AVAILABLE:
|
|
pytest.skip('pyaoaddons is installed')
|
|
|
|
monkeypatch.setattr(backend_proxy, 'NATIVE_BACKEND_AVAILABLE', False)
|
|
|
|
assert backend_proxy.initialize() == \
|
|
backend_proxy.InitializationResult.BackendNotInstalled
|
|
|
|
|
|
def test_subscribe_starts_native_backend(monkeypatch):
|
|
started = []
|
|
monkeypatch.setattr(backend_proxy, 'NATIVE_BACKEND_AVAILABLE', True)
|
|
monkeypatch.setattr(
|
|
backend_proxy, '_start_native_backend',
|
|
lambda callback: started.append(callback) or True)
|
|
|
|
callback = lambda event: event
|
|
result = backend_proxy.subscribe(callback)
|
|
|
|
assert started == [callback]
|
|
assert result is True
|
|
|
|
|
|
def test_subscribe_is_noop_without_backend(monkeypatch):
|
|
if backend_proxy.BACKEND_AVAILABLE:
|
|
pytest.skip('pyaoaddons is installed')
|
|
|
|
monkeypatch.setattr(backend_proxy, 'NATIVE_BACKEND_AVAILABLE', False)
|
|
|
|
assert backend_proxy.subscribe(lambda event: event) is False
|
|
|
|
|
|
def test_start_native_backend_uses_pipeline(monkeypatch):
|
|
import src.net.capture as capture_mod
|
|
|
|
monkeypatch.setattr(backend_proxy, '_capture', None)
|
|
|
|
created = {}
|
|
|
|
class FakeCapture:
|
|
def __init__(self, on_udp):
|
|
created['on_udp'] = on_udp
|
|
|
|
def start(self):
|
|
created['started'] = True
|
|
return True
|
|
|
|
monkeypatch.setattr(capture_mod, 'CaptureEngine', FakeCapture)
|
|
|
|
events = []
|
|
assert backend_proxy._start_native_backend(events.append) is True
|
|
|
|
assert created['started'] is True
|
|
assert callable(created['on_udp'])
|
|
assert backend_proxy._capture is not None
|
|
backend_proxy._capture = None
|
|
|
|
|
|
def test_start_native_backend_reports_start_failure(monkeypatch):
|
|
import src.net.capture as capture_mod
|
|
|
|
monkeypatch.setattr(backend_proxy, '_capture', None)
|
|
|
|
class FakeCapture:
|
|
def __init__(self, on_udp):
|
|
self.on_udp = on_udp
|
|
|
|
def start(self):
|
|
return False
|
|
|
|
monkeypatch.setattr(capture_mod, 'CaptureEngine', FakeCapture)
|
|
|
|
assert backend_proxy._start_native_backend(lambda event: event) is False
|
|
assert backend_proxy._capture is None
|
|
|
|
|
|
def test_initialize_ok_with_replay(monkeypatch):
|
|
monkeypatch.setattr(
|
|
backend_proxy.environment, 'replay_path', lambda: '/tmp/x.jsonl')
|
|
|
|
assert backend_proxy.initialize() == backend_proxy.InitializationResult.Ok
|
|
|
|
|
|
def test_subscribe_starts_replay_backend(monkeypatch):
|
|
monkeypatch.setattr(
|
|
backend_proxy.environment, 'replay_path', lambda: '/tmp/x.jsonl')
|
|
|
|
started = []
|
|
monkeypatch.setattr(
|
|
backend_proxy, '_start_replay_backend',
|
|
lambda callback: started.append(callback) or True)
|
|
|
|
callback = lambda event: event
|
|
assert backend_proxy.subscribe(callback) is True
|
|
assert started == [callback]
|
|
|
|
|
|
def test_start_replay_backend_uses_pipeline(monkeypatch):
|
|
import src.net.replay as replay_mod
|
|
|
|
monkeypatch.setattr(backend_proxy, '_replay', None)
|
|
|
|
created = {}
|
|
|
|
class FakeReplay:
|
|
def __init__(self, on_udp):
|
|
created['on_udp'] = on_udp
|
|
|
|
def start(self):
|
|
created['started'] = True
|
|
return True
|
|
|
|
monkeypatch.setattr(replay_mod, 'ReplayEngine', FakeReplay)
|
|
|
|
assert backend_proxy._start_replay_backend(lambda event: event) is True
|
|
assert created['started'] is True
|
|
assert callable(created['on_udp'])
|
|
assert backend_proxy._replay is not None
|
|
backend_proxy._replay = None
|
|
|
|
|
|
def test_start_replay_backend_reports_start_failure(monkeypatch):
|
|
import src.net.replay as replay_mod
|
|
|
|
monkeypatch.setattr(backend_proxy, '_replay', None)
|
|
|
|
class FakeReplay:
|
|
def __init__(self, on_udp):
|
|
self.on_udp = on_udp
|
|
|
|
def start(self):
|
|
return False
|
|
|
|
monkeypatch.setattr(replay_mod, 'ReplayEngine', FakeReplay)
|
|
|
|
assert backend_proxy._start_replay_backend(lambda event: event) is False
|
|
assert backend_proxy._replay is None
|
|
|
|
|
|
def test_engine_initialize_reports_subscribe_failure(monkeypatch):
|
|
monkeypatch.setattr(
|
|
engine.backend_proxy, 'initialize',
|
|
lambda: backend_proxy.InitializationResult.Ok)
|
|
monkeypatch.setattr(
|
|
engine.backend_proxy, 'subscribe',
|
|
lambda callback: False)
|
|
|
|
assert engine.initialize() == backend_proxy.InitializationResult.UnknownFailure
|