"""The uplink's state is the gateway's own: the DSL line and the WAN interfaces (#29). ``get_wan_status`` built its answer from ``WanIp``, ``WanAccessType``, ``SystemUpTime``, ``MaxUpstream`` and ``MaxDownstream`` in ``getBasicInformation``. A VMG4005-B50A sends none of them there, so every gateway's uplink read as down, with no address and no sync rate, whatever its line did. ``get_nat_translations`` built ``inside_global`` from the same ``WanIp`` and reported ``":"``. The line state is in the DAL's ``cardpage_status``: ``DslChannelInfo[]`` (a channel whose ``Status`` is ``Up`` is in sync; its rates are in kbit/s) and the ``WanLanInfo[]`` entries of ``X_ZYXEL_Type`` ``WAN``. A bridge has no WAN address of its own: there, connected means the DSL line is in sync. ``STATUS`` is that answer from a VMG4005-B50A bridge whose line was not in sync when it was recorded (``tests/gateway.py``). The other states change it the way TR-181 and the recorded fields describe them: they were not recorded. """ from __future__ import annotations import copy from typing import Any import pytest from tests.gateway import dal, driver, fixture, requested, response STATUS = fixture("cardpage_status") PTM_LINE_2 = 3 # DslChannelInfo: the channel that carries the recorded sync rates def _status( channels: dict[int, dict[str, Any]] | None = None, wan: dict[str, Any] | None = None ) -> dict[str, Any]: """The recorded status page with *channels* (index: changes) and *wan* (changes to the ``ptm0.2`` WAN interface) applied.""" answer = copy.deepcopy(STATUS) page = answer["Object"][0] for index, changes in (channels or {}).items(): page["DslChannelInfo"][index].update(changes) for entry in page["WanLanInfo"]: if entry["X_ZYXEL_IfName"] == "ptm0.2": entry.update(wan or {}) return answer IN_SYNC = _status({PTM_LINE_2: {"Status": "Up"}}, {"Status": "Up"}) ROUTED = { "X_ZYXEL_ConnectionType": "IP_Routed", "Encapsulation": "PPPoE", "Status": "Up", "X_ZYXEL_UpTime": 7200, "IPv4Address": [ { "Enable": True, "IPAddress": "203.0.113.10", "SubnetMask": "255.255.255.255", "Status": "Enabled", } ], } def _wan(answer: dict[str, Any]) -> Any: return driver({dal("cardpage_status"): answer}).get_wan_status() # --- a bridge ----------------------------------------------------------------------------- def test_a_bridge_whose_line_is_not_in_sync_is_down(): assert _wan(STATUS) == { "connection_type": "DSL", "is_connected": False, "external_ip": "", "uptime": 0, "bytes_sent": 0, "bytes_received": 0, "max_bitrate_up": 0, "max_bitrate_down": 0, "link_status": "Down", } def test_a_bridge_whose_line_is_in_sync_is_connected(): status = _wan(IN_SYNC) assert (status["is_connected"], status["link_status"]) == (True, "Up") assert status["external_ip"] == "" # a bridge has no WAN address def test_the_sync_rates_are_the_channels_in_kbit_per_second(): status = _wan(IN_SYNC) assert (status["max_bitrate_up"], status["max_bitrate_down"]) == (45875, 274098) def test_the_rates_of_a_line_out_of_sync_are_not_reported(): """The recorded channel keeps the rates of its last sync while it is down.""" status = _wan(STATUS) assert (status["max_bitrate_up"], status["max_bitrate_down"]) == (0, 0) def test_bonded_lines_add_up(): rates = {"UpstreamCurrRate": 40000, "DownstreamCurrRate": 100000} answer = _status({1: {"Status": "Up", **rates}, PTM_LINE_2: {"Status": "Up"}}) status = _wan(answer) assert (status["max_bitrate_up"], status["max_bitrate_down"]) == (85875, 374098) def test_the_line_uptime_is_reported_when_the_gateway_gives_one(): answer = _status({PTM_LINE_2: {"Status": "Up", "DSLUpTime": "5400"}}) assert _wan(answer)["uptime"] == 5400 def test_a_line_uptime_that_is_not_a_number_is_unknown(): answer = _status({PTM_LINE_2: {"Status": "Up", "DSLUpTime": ""}}) assert _wan(answer)["uptime"] == 0 # --- a router ----------------------------------------------------------------------------- def test_a_router_with_a_wan_address_is_connected_with_it(): status = _wan(_status({PTM_LINE_2: {"Status": "Up"}}, ROUTED)) assert status["is_connected"] is True assert status["external_ip"] == "203.0.113.10" assert status["connection_type"] == "PPPoE" assert status["uptime"] == 7200 def test_a_router_whose_line_is_in_sync_but_its_wan_down_is_not_connected(): wan = {**ROUTED, "Status": "Down", "IPv4Address": []} status = _wan(_status({PTM_LINE_2: {"Status": "Up"}}, wan)) assert (status["is_connected"], status["link_status"]) == (False, "Up") assert status["external_ip"] == "" def test_a_disabled_wan_address_is_not_the_external_one(): addresses = [{**ROUTED["IPv4Address"][0], "Enable": False}] status = _wan(_status({PTM_LINE_2: {"Status": "Up"}}, {**ROUTED, "IPv4Address": addresses})) assert (status["is_connected"], status["external_ip"]) == (False, "") # --- reading ------------------------------------------------------------------------------ def test_basic_information_is_not_read_for_it(): drv = driver({dal("cardpage_status"): IN_SYNC}) drv._device_info = {**fixture("getBasicInformation"), "WanIp": "198.51.100.1"} assert drv.get_wan_status()["external_ip"] == "" def test_get_wan_status_raises_when_the_status_page_cannot_be_read(): with pytest.raises(RuntimeError, match="cardpage_status"): _wan(response(500)) # type: ignore[arg-type] # --- NAT translations --------------------------------------------------------------------- RULE = { "Enable": True, "Protocol": "TCP", "Description": "web", "ExternalPortStart": 8443, "InternalPortStart": 443, "InternalClient": "192.0.2.3", } def _nat_driver(status: Any) -> Any: return driver( {dal("nat"): {**fixture("nat"), "Object": [RULE]}, dal("cardpage_status"): status} ) def test_a_translation_is_to_the_wan_address(): status = _status({PTM_LINE_2: {"Status": "Up"}}, ROUTED) nat = _nat_driver(status).get_nat_translations() assert [(t["inside_local"], t["inside_global"]) for t in nat] == [ ("192.0.2.3:443", "203.0.113.10:8443") ] def test_without_a_wan_address_a_translation_is_to_any_address(): nat = _nat_driver(STATUS).get_nat_translations() assert [t["inside_global"] for t in nat] == ["0.0.0.0:8443"] def test_without_port_forwards_the_status_page_is_not_read(): drv = driver({dal("nat"): fixture("nat")}) assert drv.get_nat_translations() == [] assert requested(drv) == ["http://192.0.2.1:80/cgi-bin/DAL?oid=nat"]