Files
DECNET/tests/asn/test_provider.py
anti f2b3393669 chore: relicense to AGPL-3.0-or-later and add SPDX headers
Replaces LICENSE (GPLv3 -> AGPLv3) and prepends
`SPDX-License-Identifier: AGPL-3.0-or-later` to every source file
across decnet/, decnet_web/, tests/, scripts/, and tools/.

Rationale: closes the GPLv3 ASP loophole so any party operating a
modified DECNET as a network service must offer their modified
source. Personal copyright (Samuel Paschuan) + inbound=outbound
contributions make a future unilateral relicense infeasible.

- LICENSE: full AGPL-3.0 text (gnu.org/licenses/agpl-3.0.txt)
- COPYRIGHT: project copyright notice
- tools/add_spdx_headers.py: idempotent header injector
  (shebang- and PEP 263-aware)

Touches 1565 source files (.py, .ts, .tsx, .js, .jsx, .css, .sh).
No behavior change; comments only.
2026-05-22 21:04:16 -04:00

98 lines
2.8 KiB
Python

# SPDX-License-Identifier: AGPL-3.0-or-later
"""IptoasnProvider + factory + public API tests."""
from __future__ import annotations
import gzip
from pathlib import Path
import pytest
def _seed_fixture(root: Path, content: str = "8.8.8.0\t8.8.8.255\t15169\tUS\tGOOGLE\n") -> None:
target = root / "ip2asn-v4.tsv.gz"
with gzip.open(target, "wt", encoding="utf-8") as fh:
fh.write(content)
def test_factory_returns_iptoasn_by_default() -> None:
from decnet.asn.factory import get_provider
provider = get_provider()
assert provider.name == "iptoasn"
def test_factory_rejects_unknown_provider(monkeypatch: pytest.MonkeyPatch) -> None:
from decnet.asn import factory
monkeypatch.setenv("DECNET_ASN_PROVIDER", "nope")
factory.reset_cache()
with pytest.raises(ValueError):
factory.get_provider()
def test_provider_build_lookup_empty_when_no_files(tmp_path: Path) -> None:
from decnet.asn.iptoasn.provider import IptoasnProvider
p = IptoasnProvider()
lookup = p.build_lookup()
assert len(lookup) == 0
assert lookup.asn("8.8.8.8") is None
def test_provider_build_lookup_reads_present_file(tmp_path: Path) -> None:
from decnet.asn.iptoasn.provider import IptoasnProvider
_seed_fixture(tmp_path)
p = IptoasnProvider()
lookup = p.build_lookup()
info = lookup.asn("8.8.8.8")
assert info is not None
assert info.asn == 15169
assert info.name == "GOOGLE"
def test_provider_uses_cache_when_fresh(tmp_path: Path) -> None:
from decnet.asn.iptoasn.provider import IptoasnProvider
_seed_fixture(tmp_path)
p = IptoasnProvider()
a = p.build_lookup()
assert (tmp_path / ".iptoasn_index.pkl").exists()
p2 = IptoasnProvider()
b = p2.build_lookup()
assert len(b) == len(a)
def test_enrich_ip_short_circuits_when_disabled(monkeypatch: pytest.MonkeyPatch) -> None:
import decnet.asn as asn
monkeypatch.setenv("DECNET_ASN_ENABLED", "false")
assert asn.enrich_ip("8.8.8.8") == (None, None, None, None)
def test_enrich_ip_returns_asn_prefix_and_source(tmp_path: Path) -> None:
from decnet.asn import enrich_ip
_seed_fixture(tmp_path)
asn, name, prefix, src = enrich_ip("8.8.8.8")
assert asn == 15169
assert name == "GOOGLE"
assert prefix == "8.8.8.0/24"
assert src == "iptoasn"
def test_enrich_ip_private_returns_none(tmp_path: Path) -> None:
from decnet.asn import enrich_ip
_seed_fixture(tmp_path)
assert enrich_ip("192.168.1.1") == (None, None, None, None)
def test_enrich_ip_unannounced_returns_none(tmp_path: Path) -> None:
from decnet.asn import enrich_ip
_seed_fixture(tmp_path)
# 9.0.0.0 isn't in our fixture range — no BGP announcement we know of.
assert enrich_ip("9.0.0.0") == (None, None, None, None)