136 lines
5.7 KiB
Python
136 lines
5.7 KiB
Python
from __future__ import annotations
|
|
|
|
import json
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
from unittest.mock import Mock, patch
|
|
|
|
from src import studio_control
|
|
from src.studio_control import (
|
|
delete_wol_device,
|
|
list_wol_devices,
|
|
normalize_mac_address,
|
|
probe_wol_devices,
|
|
send_serial_command,
|
|
send_wake_on_lan,
|
|
upsert_wol_device,
|
|
)
|
|
|
|
|
|
class StudioControlTests(unittest.TestCase):
|
|
def test_normalize_mac_accepts_common_formats(self) -> None:
|
|
self.assertEqual(normalize_mac_address("aa:bb:cc:dd:ee:ff"), "AA:BB:CC:DD:EE:FF")
|
|
self.assertEqual(normalize_mac_address("aa-bb-cc-dd-ee-ff"), "AA:BB:CC:DD:EE:FF")
|
|
self.assertEqual(normalize_mac_address("aabb.ccdd.eeff"), "AA:BB:CC:DD:EE:FF")
|
|
|
|
def test_wol_sends_magic_packet_to_broadcast(self) -> None:
|
|
sock = Mock()
|
|
sock.sendto.return_value = 102
|
|
with patch("src.studio_control.socket.socket", return_value=sock):
|
|
result = send_wake_on_lan("AA:BB:CC:DD:EE:FF", "192.168.1.255", 9)
|
|
|
|
payload, target = sock.sendto.call_args.args
|
|
self.assertEqual(target, ("192.168.1.255", 9))
|
|
self.assertEqual(payload[:6], bytes.fromhex("FF" * 6))
|
|
self.assertEqual(payload[6:12], bytes.fromhex("AABBCCDDEEFF"))
|
|
self.assertEqual(len(payload), 102)
|
|
self.assertEqual(result["bytes"], 102)
|
|
self.assertEqual(result["mac"], "AA:BB:CC:DD:EE:FF")
|
|
|
|
def test_wol_rejects_invalid_inputs(self) -> None:
|
|
with self.assertRaises(ValueError):
|
|
send_wake_on_lan("not-a-mac")
|
|
with self.assertRaises(ValueError):
|
|
send_wake_on_lan("AA:BB:CC:DD:EE:FF", "not-an-ip")
|
|
with self.assertRaises(ValueError):
|
|
send_wake_on_lan("AA:BB:CC:DD:EE:FF", port=70000)
|
|
|
|
def test_wol_device_store_supports_manual_edit_and_delete(self) -> None:
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
base_dir = Path(tmp)
|
|
saved = upsert_wol_device("NAS", "aa-bb-cc-dd-ee-ff", "192.168.1.20", base_dir)
|
|
devices = list_wol_devices(base_dir)
|
|
deleted = delete_wol_device("AA:BB:CC:DD:EE:FF", base_dir)
|
|
|
|
self.assertEqual(saved["mac"], "AA:BB:CC:DD:EE:FF")
|
|
self.assertEqual(saved["source"], "manual")
|
|
self.assertEqual(saved["ip"], "192.168.1.20")
|
|
self.assertEqual(devices[0]["name"], "NAS")
|
|
self.assertEqual(deleted["devices"], [])
|
|
|
|
def test_wol_probe_merges_neighbor_cache_and_manual_devices(self) -> None:
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
base_dir = Path(tmp)
|
|
upsert_wol_device("Studio PC", "AA:BB:CC:DD:EE:FF", "192.168.1.20", base_dir)
|
|
neighbor = {
|
|
"id": "11:22:33:44:55:66",
|
|
"name": "192.168.1.21",
|
|
"mac": "11:22:33:44:55:66",
|
|
"ip": "192.168.1.21",
|
|
"reachable": True,
|
|
"source": "neigh",
|
|
"last_seen": 1000.0,
|
|
"updated_at": 1000.0,
|
|
}
|
|
with patch("src.studio_control._ping_host", return_value=True):
|
|
with patch("src.studio_control._ip_neigh_devices", return_value=[neighbor]):
|
|
with patch("src.studio_control.time.time", return_value=1000.0):
|
|
result = probe_wol_devices(base_dir)
|
|
|
|
by_mac = {item["mac"]: item for item in result["devices"]}
|
|
self.assertEqual(result["count"], 2)
|
|
self.assertTrue(by_mac["AA:BB:CC:DD:EE:FF"]["reachable"])
|
|
self.assertEqual(by_mac["AA:BB:CC:DD:EE:FF"]["name"], "Studio PC")
|
|
self.assertEqual(by_mac["11:22:33:44:55:66"]["source"], "neigh")
|
|
|
|
def test_wol_probe_filters_virtual_interfaces_and_stale_auto_devices(self) -> None:
|
|
raw_neigh = "\n".join(
|
|
[
|
|
"10.0.0.1 dev eno1 lladdr d2:54:cd:ab:fe:73 REACHABLE",
|
|
"172.18.0.2 dev br-123456 lladdr 62:ca:f6:01:39:7f STALE",
|
|
]
|
|
)
|
|
with patch("src.studio_control._run_text", return_value=raw_neigh):
|
|
with patch.dict(studio_control.os.environ, {"WOL_PROBE_CIDR": ""}, clear=False):
|
|
devices = studio_control._ip_neigh_devices()
|
|
self.assertEqual([item["mac"] for item in devices], ["D2:54:CD:AB:FE:73"])
|
|
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
base_dir = Path(tmp)
|
|
store = base_dir / "config" / "wol_devices.json"
|
|
store.parent.mkdir(parents=True)
|
|
store.write_text(
|
|
json.dumps(
|
|
[
|
|
{
|
|
"id": "62:CA:F6:01:39:7F",
|
|
"name": "172.18.0.2",
|
|
"mac": "62:CA:F6:01:39:7F",
|
|
"ip": "172.18.0.2",
|
|
"source": "neigh",
|
|
}
|
|
]
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
with patch("src.studio_control._ping_host", return_value=False):
|
|
with patch("src.studio_control._ip_neigh_devices", return_value=devices):
|
|
result = probe_wol_devices(base_dir)
|
|
|
|
self.assertEqual([item["mac"] for item in result["devices"]], ["D2:54:CD:AB:FE:73"])
|
|
|
|
def test_wol_device_rejects_invalid_ip(self) -> None:
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
with self.assertRaises(ValueError):
|
|
upsert_wol_device("Bad", "AA:BB:CC:DD:EE:FF", "not-an-ip", Path(tmp))
|
|
|
|
def test_serial_command_rejects_unsupported_port_path(self) -> None:
|
|
with patch.object(studio_control, "termios", object()):
|
|
with self.assertRaises(ValueError):
|
|
send_serial_command("/tmp/not-serial", "AT")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|