"""Wireless writes: ``push_mac_acl`` and ``push_radio_channel`` (NAPALM/napalm-openwrt#35). Each ``uci`` step runs on an exec channel of its own, with its exit status checked: a refused step raises and names itself, nothing staged after a refusal is committed, and wifi is not restarted for a change that never landed. The device is a fake behind ``run_command``. ``uci -q show wireless`` answers in the shape OpenWrt 23.05 prints it: the stock ``default_radioN`` sections, a list option on one line with each value quoted, and a quote inside a value as ``'\\''``. The refusals are uci's and wifi's own messages: ``uci: Invalid argument`` for an option set on a section that does not exist, ``uci: I/O error`` for a commit that cannot write ``/etc/config``. """ from __future__ import annotations import shlex from unittest.mock import MagicMock, patch import pytest from napalm.base.exceptions import CommandErrorException from napalm_device_types import CommandResult from napalm_openwrt.openwrt import OpenWrtDriver SHOW = "uci -q show wireless" WIRELESS = """\ wireless.radio0=wifi-device wireless.radio0.type='mac80211' wireless.radio0.path='platform/ahb/18100000.wmac' wireless.radio0.channel='1' wireless.radio0.band='2g' wireless.radio0.htmode='HT20' wireless.radio0.cell_density='0' wireless.default_radio0=wifi-iface wireless.default_radio0.device='radio0' wireless.default_radio0.network='lan' wireless.default_radio0.mode='ap' wireless.default_radio0.ssid='CorpWiFi' wireless.default_radio0.encryption='psk2' wireless.default_radio0.key='example-passphrase' wireless.default_radio0.macfilter='deny' wireless.default_radio0.maclist='00:00:5E:00:53:01' '00:00:5E:00:53:02' wireless.radio1=wifi-device wireless.radio1.type='mac80211' wireless.radio1.path='pci0000:00/0000:00:00.0' wireless.radio1.channel='36' wireless.radio1.band='5g' wireless.radio1.htmode='VHT80' wireless.default_radio1=wifi-iface wireless.default_radio1.device='radio1' wireless.default_radio1.network='lan' wireless.default_radio1.mode='ap' wireless.default_radio1.ssid='CorpWiFi' wireless.default_radio1.encryption='psk2' wireless.default_radio1.key='example-passphrase' wireless.guest=wifi-iface wireless.guest.device='radio0' wireless.guest.network='guest' wireless.guest.mode='ap' wireless.guest.ssid='Bob'\\''s Guest' wireless.guest.encryption='none' """ INVALID_ARGUMENT = CommandResult("", "uci: Invalid argument\n", 1) IO_ERROR = CommandResult("", "uci: I/O error\n", 1) class _Ap: """``run_command`` of an AP: answers the read, refuses the commands in *refuse*.""" def __init__(self, refuse: dict[str, CommandResult] | None = None) -> None: self.refuse = refuse or {} self.commands: list[str] = [] def __call__( self, command: str, *, privileged: bool = False, timeout: float = 60, stdin: bytes | None = None, ) -> CommandResult: self.commands.append(command) if command in self.refuse: return self.refuse[command] if command == SHOW: return CommandResult(WIRELESS, "", 0) return CommandResult("", "", 0) @property def argv(self) -> list[list[str]]: return [shlex.split(c) for c in self.commands] def _driver(ap: _Ap) -> OpenWrtDriver: with patch("napalm_openwrt.openwrt.ConnectHandler"): drv = OpenWrtDriver(hostname="192.0.2.15", username="root", password="") drv.device = MagicMock() drv.run_command = ap # type: ignore[method-assign] return drv def _uci(*words: str) -> str: return shlex.join(["uci", *words]) # -- push_mac_acl ------------------------------------------------------------------------------ def test_a_blacklist_is_written_to_every_section_with_the_ssid(): ap = _Ap() _driver(ap).push_mac_acl("CorpWiFi", "blacklist", ["00:00:5E:00:53:0A"]) assert ap.commands == [ SHOW, _uci("set", "wireless.default_radio0.macfilter=deny"), _uci("delete", "wireless.default_radio0.maclist"), _uci("add_list", "wireless.default_radio0.maclist=00:00:5E:00:53:0A"), _uci("set", "wireless.default_radio1.macfilter=deny"), _uci("add_list", "wireless.default_radio1.maclist=00:00:5E:00:53:0A"), _uci("commit", "wireless"), "wifi reload", ] def test_a_whitelist_sets_allow(): ap = _Ap() _driver(ap).push_mac_acl("CorpWiFi", "whitelist", ["00:00:5E:00:53:0A", "00:00:5E:00:53:0B"]) assert _uci("set", "wireless.default_radio1.macfilter=allow") in ap.commands assert _uci("add_list", "wireless.default_radio1.maclist=00:00:5E:00:53:0B") in ap.commands def test_off_deletes_only_the_options_a_section_has(): """A delete of a missing option exits 1; it is not sent, and no step hides its status.""" ap = _Ap() _driver(ap).push_mac_acl("CorpWiFi", "off", []) assert ap.commands == [ SHOW, _uci("delete", "wireless.default_radio0.macfilter"), _uci("delete", "wireless.default_radio0.maclist"), _uci("commit", "wireless"), "wifi reload", ] def test_a_whitelist_without_macs_is_off(): ap = _Ap() _driver(ap).push_mac_acl("CorpWiFi", "whitelist", []) assert not any("macfilter=allow" in c for c in ap.commands) assert _uci("delete", "wireless.default_radio0.macfilter") in ap.commands def test_an_ssid_with_a_quote_and_a_space_is_found(): ap = _Ap() _driver(ap).push_mac_acl("Bob's Guest", "blacklist", ["00:00:5E:00:53:0A"]) assert _uci("set", "wireless.guest.macfilter=deny") in ap.commands assert not any("default_radio" in c for c in ap.commands) def test_no_section_with_the_ssid_raises_and_writes_nothing(): ap = _Ap() with pytest.raises(CommandErrorException, match="Nowhere"): _driver(ap).push_mac_acl("Nowhere", "blacklist", ["00:00:5E:00:53:0A"]) assert ap.commands == [SHOW] def test_a_failed_read_raises_and_writes_nothing(): ap = _Ap({SHOW: CommandResult("", "uci: Entry not found\n", 1)}) with pytest.raises(CommandErrorException, match="Entry not found"): _driver(ap).push_mac_acl("CorpWiFi", "blacklist", ["00:00:5E:00:53:0A"]) assert ap.commands == [SHOW] @pytest.mark.parametrize( "refused", [ _uci("set", "wireless.default_radio0.macfilter=deny"), _uci("delete", "wireless.default_radio0.maclist"), _uci("add_list", "wireless.default_radio1.maclist=00:00:5E:00:53:0A"), ], ) def test_a_refused_change_raises_naming_it_and_reverts_before_any_commit(refused): ap = _Ap({refused: INVALID_ARGUMENT}) with pytest.raises(CommandErrorException) as caught: _driver(ap).push_mac_acl("CorpWiFi", "blacklist", ["00:00:5E:00:53:0A"]) assert refused in str(caught.value) assert "Invalid argument" in str(caught.value) assert ap.commands[-1] == _uci("revert", "wireless") assert _uci("commit", "wireless") not in ap.commands assert "wifi reload" not in ap.commands # Nothing after the refused step is sent. assert ap.commands.index(refused) == len(ap.commands) - 2 def test_a_refused_commit_raises_and_does_not_reload_wifi(): ap = _Ap({_uci("commit", "wireless"): IO_ERROR}) with pytest.raises(CommandErrorException, match="I/O error"): _driver(ap).push_mac_acl("CorpWiFi", "blacklist", ["00:00:5E:00:53:0A"]) assert ap.commands[-2:] == [_uci("commit", "wireless"), _uci("revert", "wireless")] assert "wifi reload" not in ap.commands def test_a_failed_wifi_reload_raises_naming_it(): ap = _Ap({"wifi reload": CommandResult("", "Command failed: Not found\n", 1)}) with pytest.raises(CommandErrorException, match="wifi reload") as caught: _driver(ap).push_mac_acl("CorpWiFi", "blacklist", ["00:00:5E:00:53:0A"]) assert "Not found" in str(caught.value) assert ap.commands[-1] == "wifi reload" def test_a_failed_revert_keeps_the_refusal_as_the_error(): ap = _Ap( { _uci("set", "wireless.default_radio0.macfilter=deny"): INVALID_ARGUMENT, _uci("revert", "wireless"): IO_ERROR, } ) with pytest.raises(CommandErrorException, match="Invalid argument"): _driver(ap).push_mac_acl("CorpWiFi", "blacklist", ["00:00:5E:00:53:0A"]) def test_every_step_runs_as_root(): ap = MagicMock(side_effect=_Ap()) _driver(ap).push_mac_acl("CorpWiFi", "off", []) assert ap.call_args_list assert all(call.kwargs.get("privileged") for call in ap.call_args_list) def test_a_login_other_than_root_is_refused_before_anything_runs(): with patch("napalm_openwrt.openwrt.ConnectHandler"): drv = OpenWrtDriver(hostname="192.0.2.15", username="admin", password="") drv.device = MagicMock() with pytest.raises(PermissionError): drv.push_mac_acl("CorpWiFi", "off", []) drv.device.remote_conn_pre.get_transport.assert_not_called() # -- push_radio_channel ------------------------------------------------------------------------ def test_a_channel_is_set_committed_and_wifi_restarted(): """``wifi`` (a full restart), not ``wifi reload``: many radios keep the old channel.""" ap = _Ap() _driver(ap).push_radio_channel("radio1", 44) assert ap.commands == [ _uci("set", "wireless.radio1.channel=44"), _uci("commit", "wireless"), "wifi", ] def test_channel_zero_is_auto(): ap = _Ap() _driver(ap).push_radio_channel("radio0", 0) assert ap.commands[0] == _uci("set", "wireless.radio0.channel=auto") def test_a_refused_channel_raises_and_does_not_restart_wifi(): refused = _uci("set", "wireless.radio9.channel=6") ap = _Ap({refused: INVALID_ARGUMENT}) with pytest.raises(CommandErrorException) as caught: _driver(ap).push_radio_channel("radio9", 6) assert refused in str(caught.value) assert "Invalid argument" in str(caught.value) assert ap.commands == [refused, _uci("revert", "wireless")] def test_a_refused_channel_commit_does_not_restart_wifi(): ap = _Ap({_uci("commit", "wireless"): IO_ERROR}) with pytest.raises(CommandErrorException, match="I/O error"): _driver(ap).push_radio_channel("radio0", 6) assert "wifi" not in ap.commands def test_a_failed_wifi_restart_raises_naming_it(): ap = _Ap({"wifi": CommandResult("", "", 1)}) with pytest.raises(CommandErrorException, match="`wifi`.*exit status 1"): _driver(ap).push_radio_channel("radio0", 6) @pytest.mark.parametrize("mode", ["deny", "Blacklist", "disable", ""]) def test_an_unknown_mode_raises_before_anything_is_sent(mode): """A mode the driver does not know was written as a deny list (#39).""" ap = _Ap() with pytest.raises(ValueError, match=repr(mode)): _driver(ap).push_mac_acl("CorpWiFi", mode, ["00:00:5E:00:53:0A"]) assert ap.commands == []