"""set_vlan refuses a name that would break out of its CLI line (NetOrk/netork#2455). Over SSH the name goes into ``name ""`` in config mode, one line of a block sent line by line. A line break in the name ended that line and sent the rest to the switch as commands of its own; a double quote ended the name. A name with ``\\r``, ``\\n`` or ``"`` is refused before anything is sent, over REST as well, so both transports take the same names. """ from __future__ import annotations from unittest.mock import MagicMock import pytest from napalm_procurve.procurve import ProcurveDriver #: The name from the security report: a quote ends the name, a line break the line. INJECTED = 'guest"\npassword manager user-name x plaintext y\nexit\nvlan 42\n name "guest' def _driver(transport: str) -> ProcurveDriver: d = ProcurveDriver("10.0.0.40", "admin", "pw") d._transport = transport d._api = MagicMock() d._device = MagicMock() d.applied: list[str] = [] d._enter_config_mode = MagicMock() d._exit_config_mode = MagicMock() d._save_config = MagicMock() d._apply_config_lines = lambda text: d.applied.append(text) or [] return d @pytest.mark.parametrize("transport", ["ssh", "api"]) @pytest.mark.parametrize("name", [INJECTED, "a\nb", "a\rb", 'say "hi"']) def test_a_name_with_a_line_break_or_a_double_quote_is_refused(transport, name): driver = _driver(transport) with pytest.raises(ValueError, match="VLAN name"): driver.set_vlan(42, {"name": name}) assert driver.applied == [] driver._enter_config_mode.assert_not_called() driver._api.post.assert_not_called() driver._save_config.assert_not_called() def test_a_plain_name_is_sent_quoted(): driver = _driver("ssh") driver.set_vlan(42, {"name": "guest wifi"}) assert driver.applied == ['vlan 42\n name "guest wifi"\nexit'] driver._save_config.assert_called_once() def test_a_vlan_without_a_name_is_sent_without_one(): driver = _driver("ssh") driver.set_vlan(42, {}) assert driver.applied == ["vlan 42\nexit"]