Files
napalm-tplink-jetstream/tests/unit/test_cli_values.py
T
Christian Manivong ea001751cb
driver / standard (pull_request) Failing after 17s
driver / lint (pull_request) Successful in 16s
driver / types (pull_request) Successful in 59s
driver / test (3.12) (pull_request) Successful in 1m1s
driver / test (3.13) (pull_request) Successful in 53s
driver / lowest (pull_request) Successful in 27s
driver / build (pull_request) Successful in 32s
driver / security (pull_request) Successful in 1m19s
CI / driver (pull_request) Failing after 2m53s
driver / publish (pull_request) Skipped
fix(vlans): set_vlan and set_interface refuse values with line breaks (#17)
The VLAN name went into `name "<name>"`, a port's description into
`description <text>`, each one line of a block sent line by line in
config mode and then saved: a line break ended the line and the switch
ran the rest as commands, a double quote ended the VLAN name. netOrk
replays VLAN names other devices reported here (NetOrk/netork#2455).

set_vlan() now raises ValueError for a name containing \r, \n or ",
set_interface() for a description containing \r or \n, before anything
is sent.
2026-11-12 22:25:56 +01:00

67 lines
2.3 KiB
Python

"""set_vlan and set_interface refuse values that break out of their CLI line
(NetOrk/netork#2455).
The VLAN name goes into ``name "<name>"`` and a port's description into
``description <text>``, each one line of a block sent line by line in config
mode. A line break in either ended that line and sent the rest to the switch as
commands of its own, before ``copy running-config startup-config``; a double
quote ended the VLAN name. Such a value is refused before anything is sent.
"""
from __future__ import annotations
from unittest.mock import MagicMock
import pytest
from napalm_tplink_jetstream.tplink_jetstream import TPLinkJetstreamDriver
#: The name from the security report: a quote ends the name, a line break the line.
INJECTED = 'guest"\nuser name x privilege admin password y\nexit\nvlan 42\n name "guest'
@pytest.fixture
def driver():
d = TPLinkJetstreamDriver("10.0.0.1", "admin", "pw")
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
def _nothing_sent(driver) -> None:
assert driver.applied == []
driver._enter_config_mode.assert_not_called()
driver._save_config.assert_not_called()
@pytest.mark.parametrize("name", [INJECTED, "a\nb", "a\rb", 'say "hi"'])
def test_a_vlan_name_with_a_line_break_or_a_double_quote_is_refused(driver, name):
with pytest.raises(ValueError, match="VLAN name"):
driver.set_vlan(42, {"name": name})
_nothing_sent(driver)
def test_a_plain_vlan_name_is_sent_quoted(driver):
driver.set_vlan(42, {"name": "guest wifi"})
assert driver.applied == ['vlan 42\n name "guest wifi"\nexit']
@pytest.mark.parametrize("description", ["uplink\nuser name x password y", "a\rb"])
def test_a_description_with_a_line_break_is_refused(driver, description):
with pytest.raises(ValueError, match="description"):
driver.set_interface("Gi1/0/1", {"description": description})
_nothing_sent(driver)
def test_a_one_line_description_is_sent(driver):
driver.set_interface("Gi1/0/1", {"description": "to core-sw"})
assert driver.applied == ["interface gigabitEthernet 1/0/1\n description to core-sw\nexit"]