"""Discovery tells a DAL gateway from any other Zyxel device (#13).
The driver's evidence was all vendor-wide: Zyxel's enterprise OID, Zyxel OUIs and the
word "zyxel" on the web page. Every managed Zyxel device (switch, firewall, NAS, access
point) has all three, and under discovery's normalisation they made up the driver's
whole maximum: 28 of 28, confidence "high".
What only a gateway this driver can talk to has is the DAL's key exchange: an
unauthenticated ``GET /getRSAPublickKey`` that answers ``{"RSAPublicKey": ...}``. The
driver cannot log in without it (``ZyxelVMGDriver.open``).
``_netork_confidence`` mirrors how netOrk scores a driver
(``netork/jobs/tasks/discovery_fingerprint.py``: ``_score_driver``,
``_max_possible_score``, ``norm = score / max``, "high" from 0.7, "medium" from 0.4).
"""
from __future__ import annotations
import re
import pytest
from napalm_device_types import rules_max_weight
from napalm_zyxel import ZyxelVMGDriver
#: What netOrk adds for a matching sysObjectID prefix and for a matching OUI.
OID_WEIGHT = 15.0
OUI_WEIGHT = 6.0
ZYXEL_OID = "1.3.6.1.4.1.890.1.5.8.20"
ZYXEL_MAC = "00:13:49:aa:bb:cc"
OTHER_MAC = "00:00:5e:00:53:01"
SWITCH_PAGE = "
ZyXEL GS1920-24HP zyxel web configurator"
#: The gateway's landing page and its answer to ``GET /getRSAPublickKey``, as the
#: probe accumulates them (it fetches every path of a scheme into one body).
GATEWAY_PAGE = (
"Zyxel VMG4005-B50A zyxel "
'{"RSAPublicKey": "-----BEGIN PUBLIC KEY-----\\nMIIBIjANBgkq...\\n-----END PUBLIC KEY-----\\n"}'
)
def _netork_score(cls: type[ZyxelVMGDriver], oid: str | None, mac: str | None, page: str) -> float:
score = 0.0
if mac and mac[:8].upper() in (p.upper() for p in cls.OUI_PREFIXES):
score += OUI_WEIGHT
if oid:
if not oid.startswith(cls.SNMP_OBJECT_ID_PREFIX):
return 0.0
score += OID_WEIGHT
combined = re.sub(r"\s+", " ", page.lower())
for rule in cls.HTTP_FINGERPRINT:
matched = rule.pattern in combined
if rule.mandatory and not matched:
return 0.0
if matched:
score += -rule.weight if rule.negative else rule.weight
return score
def _netork_max(cls: type[ZyxelVMGDriver]) -> float:
total = sum(spec.weight for spec in cls.PORT_SPECS or [])
total += OID_WEIGHT if cls.SNMP_OBJECT_ID_PREFIX else 0.0
total += OUI_WEIGHT if cls.OUI_PREFIXES else 0.0
rules = (*cls.SNMP_FINGERPRINT, *cls.SSH_FINGERPRINT, *cls.HTTP_FINGERPRINT)
return total + rules_max_weight(rules) or 1.0
def _netork_confidence(oid: str | None, mac: str | None, page: str) -> tuple[float, str]:
norm = _netork_score(ZyxelVMGDriver, oid, mac, page) / _netork_max(ZyxelVMGDriver)
return norm, "high" if norm >= 0.7 else "medium" if norm >= 0.4 else "low"
def test_vendor_wide_evidence_alone_stays_below_high():
"""A Zyxel switch: Zyxel OID, Zyxel OUI, "zyxel" on its page."""
norm, confidence = _netork_confidence(ZYXEL_OID, ZYXEL_MAC, SWITCH_PAGE)
assert norm < 0.7
assert confidence != "high"
def test_a_gateway_with_snmp_scores_the_maximum():
assert _netork_confidence(ZYXEL_OID, ZYXEL_MAC, GATEWAY_PAGE) == (1.0, "high")
def test_a_gateway_without_snmp_is_still_high():
"""Residential gateways often answer no SNMP; the DAL answer carries them."""
assert _netork_confidence(None, ZYXEL_MAC, GATEWAY_PAGE)[1] == "high"
def test_the_dal_answer_outweighs_snmp_and_oui_together():
"""A gateway behind a router (no OUI) without SNMP beats a switch with both."""
gateway, _ = _netork_confidence(None, OTHER_MAC, GATEWAY_PAGE)
switch, _ = _netork_confidence(ZYXEL_OID, ZYXEL_MAC, SWITCH_PAGE)
assert gateway > switch
@pytest.mark.parametrize(("scheme", "port"), [("http", 80), ("https", 443)])
def test_discovery_fetches_the_dal_key_exchange_without_counting_the_port(scheme, port):
specs = [s for s in ZyxelVMGDriver.PORT_SPECS or [] if (s.scheme, s.port) == (scheme, port)]
assert [(s.paths, s.weight, s.mandatory) for s in specs] == [
(("/getRSAPublickKey",), 0.0, False)
]
def test_the_probed_path_is_the_one_the_driver_logs_in_with():
assert ZyxelVMGDriver._RSA_KEY_PATH == "getRSAPublickKey"