"""SSIDs, radios and wireless clients of a UniFi access point.""" from __future__ import annotations from typing import Any import pytest from conftest import load from napalm_ubiquiti.parsers.wireless import ( frequency_ghz, parse_radios, parse_ssids, parse_station_list, parse_vap_stations, ) AP_MAC = "80:2a:a8:00:01:02" class TestSsids: def test_one_ssid_on_two_bands_gets_a_key_per_band(self, uap: dict[str, Any]) -> None: ssids = parse_ssids(uap) assert set(ssids) == {"PhG (5GHz)", "PhG (2.4GHz)"} assert ssids["PhG (5GHz)"] == { "enabled": True, "radio": "wifi1", "bssid": "80:2a:11:22:33:44", "encryption": "", "hidden": False, "clients": 9, } def test_a_unique_ssid_keeps_its_name_and_the_wlan_config_fills_security(self) -> None: record = { "vap_table": [ { "essid": "Lab", "radio": "na", "radio_name": "wifi1", "bssid": "f4:92:bf:00:00:02", "up": True, "num_sta": 2, "usage": "user", } ] } wlans = [{"name": "Lab", "security": "wpapsk", "wpa_mode": "wpa2", "hide_ssid": True}] ssid = parse_ssids(record, wlans)["Lab"] assert ssid["encryption"] == "WPA2-PSK" assert ssid["hidden"] is True @pytest.mark.parametrize( ("wlan", "encryption"), [ ({"security": "open"}, "open"), ({"security": "wpapsk", "wpa_mode": "wpa3"}, "WPA3-SAE"), ({"security": "wpapsk", "wpa_mode": "wpa2", "wpa3_transition": True}, "WPA2/WPA3"), ({"security": "wpaeap", "wpa_mode": "wpa2"}, "WPA2-Enterprise"), ({"security": "wep"}, "WEP"), ({"security": "osen"}, "osen"), ], ) def test_encryption_words(self, wlan: dict[str, Any], encryption: str) -> None: record = {"vap_table": [{"essid": "X", "radio": "ng", "usage": "user"}]} assert parse_ssids(record, [dict(wlan, name="X")])["X"]["encryption"] == encryption def test_mesh_and_scan_vaps_are_not_ssids(self) -> None: record = { "vap_table": [ {"essid": "vwire-1", "radio": "na", "usage": "downlink"}, {"essid": "", "radio": "na", "usage": "scan"}, {"essid": "Lab", "radio": "ng"}, ] } assert list(parse_ssids(record)) == ["Lab"] class TestRadios: def test_the_running_channel_comes_from_the_stats(self, uap: dict[str, Any]) -> None: radios = parse_radios(uap) assert set(radios) == {"wifi0", "wifi1"} assert radios["wifi1"]["channel"] == 44 assert radios["wifi1"]["band"] == "5GHz" assert radios["wifi1"]["channel_width"] == 40 assert radios["wifi1"]["tx_power"] == 17 assert radios["wifi1"]["frequency"] == pytest.approx(5.22) assert radios["wifi1"]["up"] is True assert radios["wifi0"]["band"] == "2.4GHz" assert radios["wifi0"]["frequency"] == pytest.approx(2.412) def test_without_stats_the_configured_channel_and_no_runtime_state(self) -> None: record = { "radio_table": [ {"name": "wifi2", "radio": "6e", "channel": 37, "ht": "320", "max_txpower": 23} ] } radio = parse_radios(record)["wifi2"] assert radio == { "enabled": True, "band": "6GHz", "channel": 37, "channel_width": 320, "tx_power": 23, "frequency": pytest.approx(6.135), } assert "up" not in radio def test_auto_channel_without_stats_reads_as_zero(self) -> None: record = {"radio_table": [{"name": "wifi1", "radio": "na", "channel": "auto", "ht": "80"}]} radio = parse_radios(record)["wifi1"] assert radio["channel"] == 0 assert radio["frequency"] == 0.0 def test_a_disabled_radio(self) -> None: record = {"radio_table": [{"name": "wifi0", "radio": "ng", "channel": 6, "disabled": True}]} assert parse_radios(record)["wifi0"]["enabled"] is False @pytest.mark.parametrize( ("channel", "band", "ghz"), [ (1, "2.4GHz", 2.412), (14, "2.4GHz", 2.484), (36, "5GHz", 5.18), (5, "6GHz", 5.975), (0, "5GHz", 0.0), ], ) def test_frequency(self, channel: int, band: str, ghz: float) -> None: assert frequency_ghz(channel, band) == pytest.approx(ghz) class TestWirelessClients: def test_controller_clients_of_this_ap_only(self) -> None: clients = parse_station_list(load("controller/stat-sta.json"), AP_MAC) assert clients == [ { "mac": "00:00:00:00:00:01", "ssid": "SSID", "radio": "wifi1", "signal": -61, "noise": -106, "tx_rate": 216.0, "rx_rate": 400.0, "uptime": 11904, "ip": "192.168.0.1", "hostname": "client", } ] def test_another_aps_clients_are_left_out(self) -> None: assert parse_station_list(load("controller/stat-sta.json"), "f4:92:bf:00:00:99") == [] def test_a_devices_own_station_tables(self) -> None: """``mca-dump`` lists the clients under each VAP; no AP filter needed.""" record = { "vap_table": [ { "essid": "Lab", "radio": "na", "radio_name": "wifi1", "sta_table": [ { "mac": "AA:BB:CC:00:00:01", "signal": -55, "noise": -95, "tx_rate": 866700, "rx_rate": 780000, "uptime": 60, } ], } ] } clients = parse_vap_stations(record) assert clients == [ { "mac": "aa:bb:cc:00:00:01", "ssid": "Lab", "radio": "wifi1", "signal": -55, "noise": -95, "tx_rate": 866.7, "rx_rate": 780.0, "uptime": 60, } ]