Base working for damage
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
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_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
|
||||
@@ -0,0 +1,56 @@
|
||||
import os
|
||||
|
||||
from src.utils import config
|
||||
from src.utils import platform_utils
|
||||
|
||||
|
||||
def _set_os(monkeypatch, system):
|
||||
monkeypatch.setattr(platform_utils, 'system', lambda: system)
|
||||
|
||||
|
||||
def test_config_path_linux_uses_xdg_config_home(monkeypatch, tmp_path):
|
||||
_set_os(monkeypatch, platform_utils.LINUX)
|
||||
monkeypatch.setenv('XDG_CONFIG_HOME', str(tmp_path))
|
||||
|
||||
assert config.get_config_path() == os.path.join(
|
||||
str(tmp_path), platform_utils.APP_DIR_NAME, 'albion-online-stats.cfg')
|
||||
|
||||
|
||||
def test_config_path_linux_falls_back_to_home(monkeypatch, tmp_path):
|
||||
_set_os(monkeypatch, platform_utils.LINUX)
|
||||
monkeypatch.delenv('XDG_CONFIG_HOME', raising=False)
|
||||
monkeypatch.setenv('HOME', str(tmp_path))
|
||||
|
||||
assert config.get_config_path() == os.path.join(
|
||||
str(tmp_path), '.config', platform_utils.APP_DIR_NAME,
|
||||
'albion-online-stats.cfg')
|
||||
|
||||
|
||||
def test_config_path_windows_uses_appdata(monkeypatch, tmp_path):
|
||||
_set_os(monkeypatch, platform_utils.WINDOWS)
|
||||
monkeypatch.setenv('APPDATA', str(tmp_path))
|
||||
|
||||
assert config.get_config_path() == os.path.join(
|
||||
str(tmp_path), platform_utils.APP_DIR_NAME, 'albion-online-stats.cfg')
|
||||
|
||||
|
||||
def test_config_path_falls_back_to_script_dir(monkeypatch):
|
||||
_set_os(monkeypatch, 'SomethingElse')
|
||||
|
||||
assert os.path.basename(config.get_config_path()) == 'albion-online-stats.cfg'
|
||||
|
||||
|
||||
def test_config_creates_missing_directory(monkeypatch, tmp_path):
|
||||
_set_os(monkeypatch, platform_utils.LINUX)
|
||||
monkeypatch.setenv('XDG_CONFIG_HOME', str(tmp_path))
|
||||
monkeypatch.setattr(config, '_config', None)
|
||||
|
||||
expected = os.path.join(
|
||||
str(tmp_path), platform_utils.APP_DIR_NAME, 'albion-online-stats.cfg')
|
||||
|
||||
cfg = config.config()
|
||||
|
||||
assert os.path.isfile(expected)
|
||||
assert cfg['window']['width'] == 300
|
||||
|
||||
config._config = None
|
||||
@@ -0,0 +1,378 @@
|
||||
import struct
|
||||
|
||||
import pytest
|
||||
|
||||
from src.decode import photon
|
||||
from src.decode import translate
|
||||
from src.decode.photon import PhotonDecoder, decode_message18
|
||||
from src.decode.translate import into_game_message
|
||||
from src.decode.world import IdCache, World
|
||||
from src.net.capture import _extract_udp_payload
|
||||
|
||||
|
||||
# --- Protocol18 value builders -------------------------------------------------
|
||||
|
||||
def p18_short(value):
|
||||
return bytes([photon.P18['Short']]) + struct.pack('<h', value)
|
||||
|
||||
|
||||
def p18_string(value):
|
||||
data = value.encode('utf-8')
|
||||
return bytes([photon.P18['String']]) + bytes([len(data)]) + data
|
||||
|
||||
|
||||
def p18_compressed_int(value):
|
||||
zz = (value << 1) if value >= 0 else ((-value << 1) - 1)
|
||||
out = bytes([photon.P18['CompressedInt']])
|
||||
while True:
|
||||
b = zz & 0x7F
|
||||
zz >>= 7
|
||||
if zz:
|
||||
out += bytes([b | 0x80])
|
||||
else:
|
||||
return out + bytes([b])
|
||||
|
||||
|
||||
def params_table(entries):
|
||||
out = bytes([len(entries)])
|
||||
for key, encoded in entries:
|
||||
out += bytes([key]) + encoded
|
||||
return out
|
||||
|
||||
|
||||
def event_payload(opcode, entries):
|
||||
table = params_table([(252, p18_short(opcode))] + list(entries))
|
||||
return bytes([0xF3, 0x04, 0x01]) + table
|
||||
|
||||
|
||||
def flagged_event_payload(opcode, entries):
|
||||
table = params_table([(252, p18_short(opcode))] + list(entries))
|
||||
return bytes([0xF3, 0x84, 0x01]) + table
|
||||
|
||||
|
||||
def response_payload(opcode, entries):
|
||||
table = params_table([(253, p18_short(opcode))] + list(entries))
|
||||
debug = bytes([photon.P18['Null']])
|
||||
return bytes([0xF3, 0x03, 0x01]) + struct.pack('<h', 0) + debug + table
|
||||
|
||||
|
||||
def enet_packet(command, msg_bytes):
|
||||
length = 12 + len(msg_bytes)
|
||||
header = struct.pack('>HBBI', 0, 0, 1, 0) + struct.pack('>I', 0)
|
||||
command_header = struct.pack('>BBBB', command, 0, 0, 0)
|
||||
command_header += struct.pack('>I', length) + struct.pack('>I', 0)
|
||||
return header + command_header + msg_bytes
|
||||
|
||||
|
||||
def enet_unreliable_packet(command, msg_bytes):
|
||||
unreliable_seq = struct.pack('>I', 17)
|
||||
length = 12 + len(unreliable_seq) + len(msg_bytes)
|
||||
header = struct.pack('>HBBI', 0, 0, 1, 0) + struct.pack('>I', 0)
|
||||
command_header = struct.pack('>BBBB', command, 0, 0, 0)
|
||||
command_header += struct.pack('>I', length) + struct.pack('>I', 0)
|
||||
return header + command_header + unreliable_seq + msg_bytes
|
||||
|
||||
|
||||
# --- Protocol18 primitives ------------------------------------------------------
|
||||
|
||||
def test_varint_roundtrip():
|
||||
assert photon.rd_varint(bytes([0x00]), 0) == (0, 1)
|
||||
assert photon.rd_varint(bytes([0x80, 0x01]), 0) == (128, 2)
|
||||
assert photon.rd_varint(bytes([0xAC, 0x02]), 0) == (300, 2)
|
||||
|
||||
|
||||
def test_zigzag():
|
||||
assert photon.zigzag(0) == 0
|
||||
assert photon.zigzag(1) == -1
|
||||
assert photon.zigzag(2) == 1
|
||||
assert photon.zigzag(3) == -2
|
||||
|
||||
|
||||
def test_decode_params_and_compressed_int():
|
||||
table = params_table([(0, p18_compressed_int(280962)),
|
||||
(2, p18_string('bionicKatana')),
|
||||
(3, p18_compressed_int(4))])
|
||||
params, pos = photon.decode_params18(table, 0)
|
||||
assert params[0] == 280962
|
||||
assert params[2] == 'bionicKatana'
|
||||
assert params[3] == 4
|
||||
assert pos == len(table)
|
||||
|
||||
|
||||
def test_decode_message18_event():
|
||||
payload = event_payload(1, [(0, p18_compressed_int(277536))])
|
||||
message, pos = decode_message18(payload, 0)
|
||||
assert message.kind == 'Event'
|
||||
assert message.code == 1
|
||||
assert message.params[252] == 1
|
||||
assert message.params[0] == 277536
|
||||
assert pos == len(payload)
|
||||
|
||||
|
||||
def test_decode_message18_flagged_event():
|
||||
payload = flagged_event_payload(1, [(0, p18_compressed_int(277536))])
|
||||
message, pos = decode_message18(payload, 0)
|
||||
assert message.kind == 'Event'
|
||||
assert message.params[252] == 1
|
||||
assert message.params[0] == 277536
|
||||
assert pos == len(payload)
|
||||
|
||||
|
||||
# --- eNet envelope + fragments --------------------------------------------------
|
||||
|
||||
def test_decode_packet_reliable():
|
||||
decoder = PhotonDecoder()
|
||||
packet = enet_packet(
|
||||
photon.CMD_SEND_RELIABLE, event_payload(90, [(0, p18_short(7))]))
|
||||
messages = decoder.decode_packet(packet)
|
||||
assert len(messages) == 1
|
||||
assert messages[0].params[252] == 90
|
||||
|
||||
|
||||
def test_decode_packet_observed_unreliable_fragment_command():
|
||||
decoder = PhotonDecoder()
|
||||
packet = enet_unreliable_packet(
|
||||
photon.CMD_SEND_UNRELIABLE_FRAGMENT,
|
||||
flagged_event_payload(1, [(0, p18_short(7))]))
|
||||
messages = decoder.decode_packet(packet)
|
||||
assert len(messages) == 1
|
||||
assert messages[0].params[252] == 1
|
||||
assert messages[0].params[0] == 7
|
||||
|
||||
|
||||
def test_fragment_reassembly():
|
||||
decoder = PhotonDecoder()
|
||||
full = event_payload(29, [(0, p18_short(1234))])
|
||||
split = 5
|
||||
parts = [full[i:i + split] for i in range(0, len(full), split)]
|
||||
messages = []
|
||||
for i, part in enumerate(parts):
|
||||
start_seq, count = 5, len(parts)
|
||||
length = 12 + photon.FRAG_HEADER_LEN + len(part)
|
||||
header = struct.pack('>HBBI', 0, 0, 1, 0) + struct.pack('>I', 0)
|
||||
cmd = struct.pack('>BBBB', photon.CMD_SEND_FRAGMENT, 0, 0, 0)
|
||||
cmd += struct.pack('>I', length) + struct.pack('>I', 0)
|
||||
cmd += struct.pack('>IIIII', start_seq, count, i, len(full), i * split)
|
||||
messages.extend(decoder.decode_packet(header + cmd + part))
|
||||
assert len(messages) == 1
|
||||
assert messages[0].params[252] == 29
|
||||
assert messages[0].params[0] == 1234
|
||||
|
||||
|
||||
# --- translate ------------------------------------------------------------------
|
||||
|
||||
@pytest.mark.parametrize('opcode,name', [
|
||||
(1, 'Leave'),
|
||||
(6, 'HealthUpdate'),
|
||||
(29, 'NewCharacter'),
|
||||
(82, 'UpdateFame'),
|
||||
(90, 'CharacterEquipmentChanged'),
|
||||
(91, 'RegenerationHealthChanged'),
|
||||
(166, 'KnockedDown'),
|
||||
(231, 'PartyJoined'),
|
||||
(233, 'PartyPlayerJoined'),
|
||||
(235, 'PartyPlayerLeft'),
|
||||
])
|
||||
def test_event_opcode_translation(opcode, name):
|
||||
message, _ = decode_message18(event_payload(opcode, []), 0)
|
||||
game = into_game_message(message)
|
||||
assert game is not None
|
||||
assert game.name == name
|
||||
|
||||
|
||||
def test_operation_translation():
|
||||
message, _ = decode_message18(response_payload(2, [(0, p18_short(63))]), 0)
|
||||
game = into_game_message(message)
|
||||
assert game is not None
|
||||
assert game.name == 'Join'
|
||||
|
||||
|
||||
def test_unknown_opcode_ignored():
|
||||
message, _ = decode_message18(event_payload(9999, []), 0)
|
||||
assert into_game_message(message) is None
|
||||
|
||||
|
||||
# --- world ----------------------------------------------------------------------
|
||||
|
||||
def game(name, params=None):
|
||||
return translate.GameMessage(name, 0, params or {})
|
||||
|
||||
|
||||
def test_id_cache_reuses_id_for_name():
|
||||
cache = IdCache()
|
||||
cache.save(100, 'Alice')
|
||||
assert cache.get_static_id(100) == 0
|
||||
cache.save(200, 'Alice')
|
||||
assert cache.get_static_id(200) == 0
|
||||
assert cache.get_static_id(100) is None
|
||||
assert cache.get_static_id(300) is None
|
||||
|
||||
|
||||
def test_world_new_character_player_appeared():
|
||||
world = World()
|
||||
events = world.transform(game('NewCharacter', {0: 1234, 1: 'Alice'}))
|
||||
names = [e['name'] for e in events]
|
||||
assert 'PlayerAppeared' in names
|
||||
appeared = next(e for e in events if e['name'] == 'PlayerAppeared')
|
||||
assert appeared['value'] == {'id': 0, 'name': 'Alice'}
|
||||
|
||||
|
||||
def test_world_join_emits_zone_change_and_main_player():
|
||||
world = World()
|
||||
events = world.transform(game('Join', {0: 63, 2: 'Alice'}))
|
||||
assert events[0] == {'name': 'ZoneChange'}
|
||||
assert events[1]['name'] == 'MainPlayerAppeared'
|
||||
assert world.main_player_id == 0
|
||||
|
||||
second = world.transform(game('Join', {0: 77, 2: 'Alice'}))
|
||||
assert {'name': 'ZoneChange'} not in second
|
||||
|
||||
|
||||
def test_world_main_player_leave_emits_zone_change():
|
||||
world = World()
|
||||
world.transform(game('Join', {0: 63, 2: 'Alice'}))
|
||||
events = world.transform(game('Leave', {0: 63}))
|
||||
assert events == [{'name': 'ZoneChange'}]
|
||||
|
||||
|
||||
def test_world_health_update_damage_and_heal():
|
||||
world = World()
|
||||
world.transform(game('NewCharacter', {0: 10, 1: 'Victim'}))
|
||||
world.transform(game('NewCharacter', {0: 20, 1: 'Attacker'}))
|
||||
|
||||
damage = world.transform(game('HealthUpdate', {0: 10, 6: 20, 2: -80.0}))
|
||||
assert damage[0]['name'] == 'DamageDone'
|
||||
assert damage[0]['value']['source'] == 1 # attacker
|
||||
assert damage[0]['value']['target'] == 0 # victim
|
||||
assert damage[0]['value']['value'] == 80.0
|
||||
|
||||
heal = world.transform(game('HealthUpdate', {0: 10, 6: 20, 2: 40.0}))
|
||||
assert heal[0]['name'] == 'HealthReceived'
|
||||
assert heal[0]['value']['source'] == 1
|
||||
assert heal[0]['value']['value'] == 40.0
|
||||
|
||||
|
||||
def test_world_health_update_unknown_actor_dropped():
|
||||
world = World()
|
||||
world.transform(game('NewCharacter', {0: 10, 1: 'Victim'}))
|
||||
assert world.transform(game('HealthUpdate', {0: 10, 6: 999, 2: -50.0})) == []
|
||||
|
||||
|
||||
def test_world_regeneration_combat_states():
|
||||
world = World()
|
||||
world.transform(game('NewCharacter', {0: 10, 1: 'Alice'}))
|
||||
enter = world.transform(game('RegenerationHealthChanged', {0: 10}))
|
||||
assert enter[0]['name'] == 'EnterCombat'
|
||||
leave = world.transform(game('RegenerationHealthChanged', {0: 10, 4: 5.0}))
|
||||
assert leave[0]['name'] == 'LeaveCombat'
|
||||
|
||||
|
||||
def test_world_knocked_down_leaves_combat():
|
||||
world = World()
|
||||
world.transform(game('NewCharacter', {0: 10, 1: 'Alice'}))
|
||||
events = world.transform(game('KnockedDown', {0: 10}))
|
||||
assert events[0]['name'] == 'LeaveCombat'
|
||||
|
||||
|
||||
def test_world_update_fame_uses_gained():
|
||||
world = World()
|
||||
world.transform(game('NewCharacter', {0: 10, 1: 'Alice'}))
|
||||
events = world.transform(game('UpdateFame', {0: 10, 1: 5000000, 2: 30000}))
|
||||
assert events[0]['name'] == 'UpdateFame'
|
||||
assert events[0]['value']['value'] == pytest.approx(3.0)
|
||||
|
||||
|
||||
def test_world_update_fame_delta_from_total():
|
||||
world = World()
|
||||
world.transform(game('NewCharacter', {0: 10, 1: 'Alice'}))
|
||||
assert world.transform(game('UpdateFame', {0: 10, 1: 2000000})) == []
|
||||
events = world.transform(game('UpdateFame', {0: 10, 1: 2001000}))
|
||||
assert events[0]['value']['value'] == pytest.approx(0.1)
|
||||
|
||||
|
||||
def test_world_unconsumed_equipment_replayed_on_new_character():
|
||||
world = World()
|
||||
# equipment arrives before the player is named -> buffered
|
||||
assert world.transform(
|
||||
game('CharacterEquipmentChanged', {0: 55, 2: [0, 1]})) == []
|
||||
events = world.transform(game('NewCharacter', {0: 55, 1: 'Alice'}))
|
||||
names = [e['name'] for e in events]
|
||||
assert 'PlayerAppeared' in names
|
||||
assert 'UpdateItems' in names
|
||||
|
||||
|
||||
def test_party_events_from_structures():
|
||||
world = World()
|
||||
world.party.set_main_player_name('Alice')
|
||||
world.cache.save(1, 'Alice')
|
||||
joined = world.transform(
|
||||
game('PartyJoined', {4: [[1, 0], [2, 0]], 5: ['Alice', 'Bob']}))
|
||||
assert joined[0]['name'] == 'UpdateParty'
|
||||
assert joined[0]['value']['player_names'] == ['Alice', 'Bob']
|
||||
|
||||
left = world.transform(game('PartyPlayerLeft', {1: [2, 0]}))
|
||||
assert left[0]['value']['player_names'] == ['Alice']
|
||||
|
||||
|
||||
def test_party_main_player_left_disbands():
|
||||
world = World()
|
||||
world.party.set_main_player_name('Alice')
|
||||
world.transform(game('PartyJoined', {4: [[1, 0], [2, 0]],
|
||||
5: ['Alice', 'Bob']}))
|
||||
events = world.transform(game('PartyPlayerLeft', {1: [1, 0]}))
|
||||
assert events[0]['value']['player_names'] == []
|
||||
|
||||
|
||||
# --- capture frame extraction ---------------------------------------------------
|
||||
|
||||
def build_ethernet_ipv4_udp(src_port, dst_port, payload):
|
||||
udp = struct.pack('>HHHH', src_port, dst_port, 8 + len(payload), 0) + payload
|
||||
ip = bytearray(20)
|
||||
ip[0] = 0x45
|
||||
ip[2:4] = struct.pack('>H', 20 + len(udp))
|
||||
ip[9] = 17
|
||||
ip[12:16] = bytes([10, 0, 0, 1])
|
||||
ip[16:20] = bytes([10, 0, 0, 2])
|
||||
ethernet = bytes(12) + struct.pack('>H', 0x0800)
|
||||
return ethernet + bytes(ip) + udp
|
||||
|
||||
|
||||
def test_extract_udp_payload_game_port():
|
||||
frame = build_ethernet_ipv4_udp(12345, 5056, b'hello')
|
||||
assert _extract_udp_payload(frame) == b'hello'
|
||||
|
||||
|
||||
def test_extract_udp_payload_other_port_ignored():
|
||||
frame = build_ethernet_ipv4_udp(12345, 1234, b'hello')
|
||||
assert _extract_udp_payload(frame) is None
|
||||
|
||||
|
||||
def test_extract_udp_payload_with_vlan():
|
||||
inner = build_ethernet_ipv4_udp(5056, 12345, b'world')
|
||||
header = inner[:12]
|
||||
tagged = header + struct.pack('>H', 0x8100) + struct.pack('>H', 42) + inner[12:]
|
||||
assert _extract_udp_payload(tagged) == b'world'
|
||||
|
||||
|
||||
def build_ethernet_ipv6_udp(src_port, dst_port, payload):
|
||||
udp = struct.pack('>HHHH', src_port, dst_port, 8 + len(payload), 0) + payload
|
||||
ip = bytearray(40)
|
||||
ip[0] = 0x60
|
||||
ip[6] = 17
|
||||
ip[8:24] = bytes.fromhex('fe800000000000000000000000000001')
|
||||
ip[24:40] = bytes.fromhex('fe800000000000000000000000000002')
|
||||
ethernet = bytes(12) + struct.pack('>H', 0x86DD)
|
||||
return ethernet + bytes(ip) + udp
|
||||
|
||||
|
||||
def test_extract_udp_payload_ipv6():
|
||||
frame = build_ethernet_ipv6_udp(5056, 12345, b'v6')
|
||||
assert _extract_udp_payload(frame) == b'v6'
|
||||
|
||||
|
||||
def test_interface_names_excludes_loopback():
|
||||
from src.net.capture import CaptureEngine
|
||||
|
||||
names = CaptureEngine.interface_names()
|
||||
assert isinstance(names, list)
|
||||
assert 'lo' not in names
|
||||
@@ -0,0 +1,57 @@
|
||||
import os
|
||||
import re
|
||||
from collections import Counter
|
||||
|
||||
import pytest
|
||||
|
||||
from src.decode.pipeline import Pipeline
|
||||
|
||||
LOG_PATH = os.path.join(
|
||||
os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'aoaddons.log')
|
||||
|
||||
pytestmark = pytest.mark.skipif(
|
||||
not os.path.exists(LOG_PATH),
|
||||
reason='aoaddons.log capture fixture is not available')
|
||||
|
||||
|
||||
def _raw_payloads():
|
||||
payloads = []
|
||||
pattern = re.compile(r'Raw payload: \[([0-9, ]+)\]')
|
||||
with open(LOG_PATH, encoding='utf-8', errors='replace') as log:
|
||||
for line in log:
|
||||
match = pattern.search(line)
|
||||
if match:
|
||||
payloads.append(
|
||||
bytes(int(part) for part in match.group(1).split(',')))
|
||||
return payloads
|
||||
|
||||
|
||||
def test_offline_capture_produces_expected_events():
|
||||
payloads = _raw_payloads()
|
||||
assert payloads, 'no raw payloads found in aoaddons.log'
|
||||
|
||||
events = []
|
||||
pipeline = Pipeline(events.append)
|
||||
for payload in payloads:
|
||||
pipeline.on_udp(payload)
|
||||
|
||||
counts = Counter(event['name'] for event in events)
|
||||
assert counts['MainPlayerAppeared'] >= 1
|
||||
assert counts['PlayerAppeared'] == 2
|
||||
assert counts['ZoneChange'] == 3
|
||||
assert counts['UpdateItems'] >= 4
|
||||
|
||||
|
||||
def test_offline_capture_main_player_stream_is_consistent():
|
||||
events = []
|
||||
pipeline = Pipeline(events.append)
|
||||
for payload in _raw_payloads():
|
||||
pipeline.on_udp(payload)
|
||||
|
||||
assert any(e['name'] == 'ZoneChange' for e in events)
|
||||
main = next(e for e in events if e['name'] == 'MainPlayerAppeared')
|
||||
assert main['value']['name']
|
||||
|
||||
appeared = [e['value']['id'] for e in events
|
||||
if e['name'] == 'PlayerAppeared']
|
||||
assert appeared == [0, 1]
|
||||
@@ -0,0 +1,43 @@
|
||||
from src.utils import platform_utils
|
||||
|
||||
|
||||
def test_capture_permissions_ok_when_root(monkeypatch):
|
||||
monkeypatch.setattr(platform_utils, 'is_linux', lambda: True)
|
||||
monkeypatch.setattr(platform_utils.os, 'geteuid', lambda: 0)
|
||||
assert platform_utils.capture_permissions_ok() is True
|
||||
|
||||
|
||||
def test_capture_permissions_ok_when_socket_opens(monkeypatch):
|
||||
monkeypatch.setattr(platform_utils, 'is_linux', lambda: True)
|
||||
monkeypatch.setattr(platform_utils.os, 'geteuid', lambda: 1000)
|
||||
monkeypatch.setattr(
|
||||
platform_utils.socket,
|
||||
'socket',
|
||||
lambda *a, **k: _StubSocket(raise_error=False),
|
||||
)
|
||||
assert platform_utils.capture_permissions_ok() is True
|
||||
|
||||
|
||||
def test_capture_permissions_ok_when_permission_denied(monkeypatch):
|
||||
monkeypatch.setattr(platform_utils, 'is_linux', lambda: True)
|
||||
monkeypatch.setattr(platform_utils.os, 'geteuid', lambda: 1000)
|
||||
monkeypatch.setattr(
|
||||
platform_utils.socket,
|
||||
'socket',
|
||||
lambda *a, **k: _StubSocket(raise_error=True),
|
||||
)
|
||||
assert platform_utils.capture_permissions_ok() is False
|
||||
|
||||
|
||||
def test_capture_permissions_ok_skips_probe_on_other_platforms(monkeypatch):
|
||||
monkeypatch.setattr(platform_utils, 'is_linux', lambda: False)
|
||||
assert platform_utils.capture_permissions_ok() is True
|
||||
|
||||
|
||||
class _StubSocket:
|
||||
def __init__(self, raise_error=False):
|
||||
self._raise_error = raise_error
|
||||
|
||||
def close(self):
|
||||
if self._raise_error:
|
||||
raise PermissionError('Operation not permitted')
|
||||
Reference in New Issue
Block a user