1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
|
"""Test discovery of Ubiquiti airOS devices."""
import asyncio
from collections.abc import Callable
import os
import socket
from typing import Any, cast
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from airos.discovery import (
DISCOVERY_PORT,
AirOSDiscoveryProtocol,
airos_discover_devices,
)
from airos.exceptions import AirOSDiscoveryError, AirOSEndpointError, AirOSListenerError
# pylint: disable=redefined-outer-name
# Helper to load binary fixture
async def _read_binary_fixture(fixture_name: str) -> bytes:
"""Read a binary fixture file."""
fixture_dir = os.path.join(os.path.dirname(__file__), "../fixtures")
path = os.path.join(fixture_dir, fixture_name)
try:
def _read_file() -> bytes:
"""Read the fixture file."""
with open(path, "rb") as f:
return f.read()
return await asyncio.to_thread(_read_file)
except FileNotFoundError:
pytest.fail(f"Fixture file not found: {path}")
except OSError as e:
pytest.fail(f"Error reading fixture file {path}: {e}")
@pytest.fixture
async def mock_airos_packet() -> bytes:
"""Fixture for a valid airos discovery packet with scrubbed data."""
return await _read_binary_fixture("airos_sta_discovery_packet.bin")
@pytest.mark.asyncio
async def test_parse_airos_packet_success(mock_airos_packet: bytes) -> None:
"""Test parse_airos_packet with a valid packet containing scrubbed data."""
protocol = AirOSDiscoveryProtocol(
AsyncMock()
) # Callback won't be called directly in this unit test
host_ip = (
"192.168.1.3" # The IP address from the packet sender (as per scrubbed data)
)
# Directly call the parsing method
parsed_data = protocol.parse_airos_packet(mock_airos_packet, host_ip)
assert parsed_data is not None
assert parsed_data["ip_address"] == "192.168.1.3"
assert parsed_data["mac_address"] == "01:23:45:67:89:CD" # Expected scrubbed MAC
assert parsed_data["hostname"] == "name" # Expected scrubbed hostname
assert parsed_data["model"] == "NanoStation 5AC loco"
assert parsed_data["firmware_version"] == "WA.V8.7.17"
assert parsed_data["uptime_seconds"] == 265375
assert parsed_data["ssid"] == "DemoSSID"
assert parsed_data["full_model_name"] == "NanoStation 5AC loco"
@pytest.mark.asyncio
async def test_parse_airos_packet_invalid_header() -> None:
"""Test parse_airos_packet with an invalid header."""
protocol = AirOSDiscoveryProtocol(AsyncMock())
invalid_data = b"\x00\x00\x00\x00\x00\x00" + b"someotherdata"
host_ip = "192.168.1.100"
# Patch the _LOGGER.debug to verify the log message
with patch("airos.discovery._LOGGER.debug") as mock_log_debug:
with pytest.raises(AirOSEndpointError):
protocol.parse_airos_packet(invalid_data, host_ip)
mock_log_debug.assert_called_once()
assert (
"does not start with expected AirOS header"
in mock_log_debug.call_args[0][0]
)
@pytest.mark.asyncio
async def test_parse_airos_packet_too_short() -> None:
"""Test parse_airos_packet with data too short for header."""
protocol = AirOSDiscoveryProtocol(AsyncMock())
too_short_data = b"\x01\x06\x00"
host_ip = "192.168.1.100"
# Patch the _LOGGER.debug to verify the log message
with patch("airos.discovery._LOGGER.debug") as mock_log_debug:
with pytest.raises(AirOSEndpointError):
protocol.parse_airos_packet(too_short_data, host_ip)
mock_log_debug.assert_called_once()
assert (
"Packet too short for initial fixed header"
in mock_log_debug.call_args[0][0]
)
@pytest.mark.asyncio
async def test_parse_airos_packet_truncated_tlv() -> None:
"""Test parse_airos_packet with a truncated TLV."""
protocol = AirOSDiscoveryProtocol(AsyncMock())
# Header + MAC TLV (valid) + then a truncated TLV_IP
truncated_data = (
b"\x01\x06\x00\x00\x00\x00" # Header
b"\x06"
+ bytes.fromhex("0123456789CD") # Valid MAC (scrubbed)
+ b"\x02\x00" # TLV type 0x02, followed by only 1 byte for length (should be 2)
)
host_ip = "192.168.1.100"
# Expect AirOSEndpointError due to struct.error or IndexError
with pytest.raises(AirOSEndpointError):
protocol.parse_airos_packet(truncated_data, host_ip)
@pytest.mark.asyncio
async def test_datagram_received_calls_callback(mock_airos_packet: bytes) -> None:
"""Test that datagram_received correctly calls the callback."""
mock_callback = AsyncMock()
protocol = AirOSDiscoveryProtocol(mock_callback)
host_ip = "192.168.1.3" # Sender IP
with patch("asyncio.create_task") as mock_create_task:
protocol.datagram_received(mock_airos_packet, (host_ip, DISCOVERY_PORT))
# Verify the task was created and get the coroutine
mock_create_task.assert_called_once()
task_coro = mock_create_task.call_args[0][0]
# Manually await the coroutine to test the callback
await task_coro
mock_callback.assert_called_once()
called_args, _ = mock_callback.call_args
parsed_data = called_args[0]
assert parsed_data["ip_address"] == "192.168.1.3"
assert parsed_data["mac_address"] == "01:23:45:67:89:CD" # Verify scrubbed MAC
@pytest.mark.asyncio
async def test_datagram_received_handles_parsing_error() -> None:
"""Test datagram_received handles exceptions during parsing."""
mock_callback = AsyncMock()
protocol = AirOSDiscoveryProtocol(mock_callback)
invalid_data = b"\x00\x00" # Too short, will cause parsing error
host_ip = "192.168.1.100"
with patch("airos.discovery._LOGGER.exception") as mock_log_exception:
# datagram_received catches errors internally and re-raises AirOSDiscoveryError
with pytest.raises(AirOSDiscoveryError):
protocol.datagram_received(invalid_data, (host_ip, DISCOVERY_PORT))
mock_callback.assert_not_called()
mock_log_exception.assert_called_once() # Ensure exception is logged
@pytest.mark.asyncio
async def test_connection_made_sets_transport() -> None:
"""Test connection_made sets up transport and socket options."""
protocol = AirOSDiscoveryProtocol(AsyncMock())
mock_transport = MagicMock(spec=asyncio.DatagramTransport)
mock_sock = MagicMock(spec=socket.socket) # Corrected: socket import added
mock_transport.get_extra_info.return_value = mock_sock
with patch("airos.discovery._LOGGER.debug") as mock_log_debug:
protocol.connection_made(mock_transport)
assert protocol.transport is mock_transport
mock_sock.setsockopt.assert_any_call(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
mock_sock.setsockopt.assert_any_call(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
mock_log_debug.assert_called_once()
@pytest.mark.asyncio
async def test_connection_lost_without_exception() -> None:
"""Test connection_lost without an exception."""
protocol = AirOSDiscoveryProtocol(AsyncMock())
with patch("airos.discovery._LOGGER.debug") as mock_log_debug:
protocol.connection_lost(None)
mock_log_debug.assert_called_once_with(
"AirOSDiscoveryProtocol connection lost."
)
@pytest.mark.asyncio
async def test_connection_lost_with_exception() -> None:
"""Test connection_lost with an exception."""
protocol = AirOSDiscoveryProtocol(AsyncMock())
test_exception = Exception("Test connection lost error")
with (
patch("airos.discovery._LOGGER.exception") as mock_log_exception,
pytest.raises(
AirOSDiscoveryError
), # connection_lost now re-raises AirOSDiscoveryError
):
protocol.connection_lost(test_exception)
mock_log_exception.assert_called_once()
@pytest.mark.asyncio
async def test_error_received() -> None:
"""Test error_received logs the error."""
protocol = AirOSDiscoveryProtocol(AsyncMock())
test_exception = Exception("Test network error")
with patch("airos.discovery._LOGGER.error") as mock_log_error:
protocol.error_received(test_exception)
mock_log_error.assert_called_once_with(
f"UDP error received in AirOSDiscoveryProtocol: {test_exception}"
)
# Front-end discovery tests
@pytest.mark.asyncio
async def test_async_discover_devices_success(
mock_airos_packet: bytes,
mock_datagram_endpoint: tuple[asyncio.DatagramTransport, AirOSDiscoveryProtocol],
) -> None:
"""Test the high-level discovery function on a successful run."""
mock_transport, mock_protocol_instance = mock_datagram_endpoint
discovered_devices = {}
def mock_protocol_factory(callback: Callable[[Any], None]) -> MagicMock:
def inner_callback(device_info: dict[str, Any]) -> None:
"""Inner callback to process discovered device info."""
mac_address = device_info.get("mac_address")
if mac_address:
discovered_devices[mac_address] = device_info
return MagicMock(callback=inner_callback)
with patch(
"airos.discovery.AirOSDiscoveryProtocol",
new=MagicMock(side_effect=mock_protocol_factory),
):
async def _simulate_discovery() -> None:
"""Simulate the discovery process by sending a mock packet."""
await asyncio.sleep(0.1)
protocol = AirOSDiscoveryProtocol(
MagicMock()
) # Create a real protocol instance just for parsing
parsed_data = protocol.parse_airos_packet(mock_airos_packet, "192.168.1.3")
mock_protocol_factory(MagicMock()).callback(parsed_data)
with patch("airos.discovery.asyncio.sleep", new=AsyncMock()):
discovery_task = asyncio.create_task(airos_discover_devices(timeout=1))
await _simulate_discovery()
await discovery_task
assert "01:23:45:67:89:CD" in discovered_devices
assert discovered_devices["01:23:45:67:89:CD"]["hostname"] == "name"
close_mock = cast(MagicMock, mock_transport.close)
close_mock.assert_called_once()
@pytest.mark.asyncio
async def test_async_discover_devices_no_devices(
mock_datagram_endpoint: tuple[asyncio.DatagramTransport, AirOSDiscoveryProtocol],
) -> None:
"""Test discovery returns an empty dict if no devices are found."""
mock_transport, _ = mock_datagram_endpoint
with patch("airos.discovery.asyncio.sleep", new=AsyncMock()):
result = await airos_discover_devices(timeout=1)
assert result == {}
close_mock = cast(MagicMock, mock_transport.close)
close_mock.assert_called_once()
@pytest.mark.asyncio
async def test_async_discover_devices_oserror(
mock_datagram_endpoint: tuple[asyncio.DatagramTransport, AirOSDiscoveryProtocol],
) -> None:
"""Test discovery handles OSError during endpoint creation."""
mock_transport, _ = mock_datagram_endpoint
with patch("asyncio.get_running_loop") as mock_get_loop:
mock_loop = mock_get_loop.return_value
mock_loop.create_datagram_endpoint = AsyncMock(
side_effect=OSError(98, "Address in use")
)
with pytest.raises(AirOSEndpointError) as excinfo:
await airos_discover_devices(timeout=1)
assert "address_in_use" in str(excinfo.value)
close_mock = cast(MagicMock, mock_transport.close)
close_mock.assert_not_called()
@pytest.mark.asyncio
async def test_async_discover_devices_cancelled(
mock_datagram_endpoint: tuple[asyncio.DatagramTransport, AirOSDiscoveryProtocol],
) -> None:
"""Test discovery handles CancelledError during the timeout."""
mock_transport, _ = mock_datagram_endpoint
# Patch asyncio.sleep to immediately raise CancelledError
with (
patch("asyncio.sleep", new=AsyncMock(side_effect=asyncio.CancelledError)),
pytest.raises(AirOSListenerError) as excinfo,
):
await airos_discover_devices(timeout=1)
assert "cannot_connect" in str(excinfo.value)
close_mock = cast(MagicMock, mock_transport.close)
close_mock.assert_called_once()
@pytest.mark.asyncio
async def test_datagram_received_handles_general_exception() -> None:
"""Test datagram_received handles a generic exception during parsing."""
mock_callback = AsyncMock()
protocol = AirOSDiscoveryProtocol(mock_callback)
some_data = b"\x01\x06\x00\x00\x00\x00"
host_ip = "192.168.1.100"
with (
patch.object(
protocol, "parse_airos_packet", side_effect=ValueError("A generic error")
) as mock_parse,
patch("airos.discovery._LOGGER.exception") as mock_log_exception,
):
# A generic exception should be caught and re-raised as AirOSDiscoveryError
with pytest.raises(AirOSDiscoveryError):
protocol.datagram_received(some_data, (host_ip, DISCOVERY_PORT))
mock_parse.assert_called_once_with(some_data, host_ip)
mock_callback.assert_not_called()
mock_log_exception.assert_called_once()
assert (
"Error processing AirOS discovery packet"
in mock_log_exception.call_args[0][0]
)
@pytest.mark.parametrize(
("packet_fragment", "error_message"),
[
# Case 1: TLV type 0x0A (Uptime) with wrong length
(b"\x0a\x00\x02\x01\x02", "Unexpected length for Uptime (Type 0x0A)"),
# Case 2: TLV declared length exceeds remaining packet data
(b"\x0c\x00\xff\x41\x42", "length 255 exceeds remaining data"),
# Case 3: An unknown TLV type
(b"\xff\x01\x02", "Unhandled TLV type: 0xff"),
],
)
@pytest.mark.asyncio
async def test_parse_airos_packet_tlv_edge_cases(
packet_fragment: bytes, error_message: str
) -> None:
"""Test parsing of various malformed TLV entries."""
protocol = AirOSDiscoveryProtocol(AsyncMock())
# A valid header is required to get to the TLV parsing stage
base_packet = b"\x01\x06\x00\x00\x00\x00"
malformed_packet = base_packet + packet_fragment
host_ip = "192.168.1.100"
with pytest.raises(AirOSEndpointError) as excinfo:
protocol.parse_airos_packet(malformed_packet, host_ip)
assert error_message in str(excinfo.value)
@pytest.mark.asyncio
async def test_async_discover_devices_generic_oserror(
mock_datagram_endpoint: tuple[asyncio.DatagramTransport, AirOSDiscoveryProtocol],
) -> None:
"""Test discovery handles a generic OSError during endpoint creation."""
mock_transport, _ = mock_datagram_endpoint
with patch("asyncio.get_running_loop") as mock_get_loop:
mock_loop = mock_get_loop.return_value
# Simulate an OSError that is NOT 'address in use'
mock_loop.create_datagram_endpoint = AsyncMock(
side_effect=OSError(13, "Permission denied")
)
with pytest.raises(AirOSEndpointError) as excinfo:
await airos_discover_devices(timeout=1)
assert "cannot_connect" in str(excinfo.value)
close_mock = cast(MagicMock, mock_transport.close)
close_mock.assert_not_called()
@pytest.mark.asyncio
async def test_parse_airos_packet_short_for_next_tlv() -> None:
"""Test parsing stops gracefully after the MAC TLV when no more data exists."""
protocol = AirOSDiscoveryProtocol(AsyncMock())
# Header + valid MAC TLV, but then abruptly ends
data_with_fragment = (
b"\x01\x06\x00\x00\x00\x00" + b"\x06" + bytes.fromhex("0123456789CD")
)
host_ip = "192.168.1.100"
with patch("airos.discovery._LOGGER.debug") as mock_log_debug:
parsed_data = protocol.parse_airos_packet(data_with_fragment, host_ip)
assert parsed_data is not None
assert parsed_data["mac_address"] == "01:23:45:67:89:CD"
# The debug log for the successfully parsed MAC address should be called exactly once.
mock_log_debug.assert_called_once_with(
"Parsed MAC from type 0x06: 01:23:45:67:89:CD"
)
@pytest.mark.asyncio
async def test_parse_airos_packet_truncated_two_byte_tlv() -> None:
"""Test parsing with a truncated 2-byte length field TLV."""
protocol = AirOSDiscoveryProtocol(AsyncMock())
# Header + valid MAC TLV, then a valid type (0x0a) but a truncated length field
data_with_fragment = (
b"\x01\x06\x00\x00\x00\x00"
b"\x06"
+ bytes.fromhex("0123456789CD")
+ b"\x0a\x00" # TLV type 0x0a, followed by only 1 byte for length (should be 2)
)
host_ip = "192.168.1.100"
with patch("airos.discovery._LOGGER.warning") as mock_log_warning:
with pytest.raises(AirOSEndpointError):
protocol.parse_airos_packet(data_with_fragment, host_ip)
mock_log_warning.assert_called_once()
assert "no 2-byte length field" in mock_log_warning.call_args[0][0]
@pytest.mark.asyncio
async def test_parse_airos_packet_malformed_tlv_length() -> None:
"""Test parsing with a malformed TLV length field."""
protocol = AirOSDiscoveryProtocol(AsyncMock())
# Header + valid MAC TLV, then a valid type (0x02) but a truncated length field
data_with_fragment = (
b"\x01\x06\x00\x00\x00\x00"
b"\x06"
+ bytes.fromhex("0123456789CD")
+ b"\x02\x00" # TLV type 0x02, followed by only 1 byte for length (should be 2)
)
host_ip = "192.168.1.100"
with patch("airos.discovery._LOGGER.warning") as mock_log_warning:
with pytest.raises(AirOSEndpointError):
protocol.parse_airos_packet(data_with_fragment, host_ip)
mock_log_warning.assert_called_once()
assert "no 2-byte length field" in mock_log_warning.call_args[0][0]
@pytest.mark.parametrize(
("packet_fragment", "unhandled_type"),
[
(b"\x0e\x00\x02\x01\x02", "0xe"), # Unhandled 2-byte length TLV
(b"\x10\x00\x02\x01\x02", "0x10"), # Unhandled 2-byte length TLV
],
)
@pytest.mark.asyncio
async def test_parse_airos_packet_unhandled_tlv_continues_parsing(
packet_fragment: bytes, unhandled_type: str
) -> None:
"""Test that the parser logs an unhandled TLV type but continues parsing the packet."""
protocol = AirOSDiscoveryProtocol(AsyncMock())
# Construct a packet with a valid MAC TLV followed by the unhandled TLV
valid_mac_tlv = b"\x06" + bytes.fromhex("0123456789CD")
base_packet = b"\x01\x06\x00\x00\x00\x00"
# This new packet structure ensures two TLVs are present
malformed_packet = base_packet + valid_mac_tlv + packet_fragment
host_ip = "192.168.1.100"
with patch("airos.discovery._LOGGER.debug") as mock_log_debug:
parsed_data = protocol.parse_airos_packet(malformed_packet, host_ip)
assert parsed_data is not None
assert parsed_data["mac_address"] == "01:23:45:67:89:CD"
# Now, two debug logs are expected: one for the MAC and one for the unhandled TLV.
assert mock_log_debug.call_count == 2
# Check the first log call for the MAC address
assert (
mock_log_debug.call_args_list[0][0][0]
== "Parsed MAC from type 0x06: 01:23:45:67:89:CD"
)
# Check the second log call for the unhandled TLV
log_message = mock_log_debug.call_args_list[1][0][0]
assert f"Unhandled TLV type: {unhandled_type}" in log_message
assert "with length" in log_message
@pytest.mark.asyncio
async def test_async_discover_devices_generic_exception(
mock_datagram_endpoint: tuple[asyncio.DatagramTransport, AirOSDiscoveryProtocol],
) -> None:
"""Test discovery handles a generic exception during the main execution."""
mock_transport, _ = mock_datagram_endpoint
with (
patch(
"asyncio.sleep", new=AsyncMock(side_effect=Exception("Unexpected error"))
),
patch("airos.discovery._LOGGER.exception") as mock_log_exception,
pytest.raises(AirOSListenerError) as excinfo,
):
await airos_discover_devices(timeout=1)
assert "cannot_connect" in str(excinfo.value)
mock_log_exception.assert_called_once()
close_mock = cast(MagicMock, mock_transport.close)
close_mock.assert_called_once()
|