mdns-termux/tests/test_reverse_hosts.py

30 lines
1.0 KiB
Python
Raw Normal View History

feat: publisher-daemon (singleton + re-announce), sync-hosts, status/stop y tests Bloque de robustez operativa + integracion de sistema + calidad: Publisher como servicio (lib/mdns_tools/service.py): - `mdns publish --daemon`: daemon singleton con lock PID en ~/.cache/mdns/publisher.pid. Un segundo intento es rechazado -> evita el problema de "Termux SMB" duplicado (varios publishers compitiendo por el mismo nombre, que zeroconf renombra a "-2"). - Re-anuncio automatico al cambiar la IP local (WiFi<->datos, roaming): unregister + register con la nueva IP para que el registro no quede stale. - Apagado limpio: maneja SIGTERM ademas de SIGINT (loop en ticks de 1s) y envia goodbye packets para que los clientes borren la entrada al instante. - `mdns status` (running/host/ip/servicios/desde) y `mdns stop`. Integracion de sistema (lib/mdns_tools/hosts.py): - `mdns sync-hosts [--dry-run]`: escribe un bloque gestionado en $PREFIX/etc/hosts (o /etc/hosts) para que ping/curl/kubectl nativos resuelvan .local sin el wrapper `resolve`. Solo toca su propio bloque. Refactor publish.py: expone helpers reutilizables (local_ipv4, load_config, build_infos, register_all, unregister_all) que consume el daemon. Tests offline (tests/, pytest, 20 casos, sin red): - construccion/parseo de queries A, seleccion de IP ante multi-homing (rechazo de rangos virtuales), cache con TTL y familias, PTR reverse, render/strip del bloque de hosts. - pyproject.toml con metadata, deps, extras [test] y config de pytest. README: seccion de daemon, sync-hosts y tests; tabla RFC ampliada. bashrc.snippet: aliases mdns-up/status/down/hosts (consolidados). install.sh: fix de backticks; symlinks ya cubren mdns + lib via resolve(). Nota: publish.py (109) y service.py (110) exceden levemente las 100 lineas; se dejan como modulos cohesivos de responsabilidad unica, igual que el dispatcher CLI, para no fragmentar. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-07-20 17:59:10 +00:00
"""Tests for reverse-pointer naming and hosts-file block management."""
from mdns_tools import reverse as rv
from mdns_tools import hosts as h
def test_reverse_name_ipv4():
arpa, fam = rv._reverse_name("192.168.1.17")
assert arpa == "17.1.168.192.in-addr.arpa"
assert fam == "v4"
def test_reverse_name_ipv6_family():
_, fam = rv._reverse_name("2800:e2::5")
assert fam == "v6"
def test_hosts_block_render_and_strip_roundtrip():
mapping = {"dell.local": "192.168.1.17", "hp62a.local": "192.168.1.16"}
block = h._render_block(mapping)
assert h.MARK_BEGIN in block and h.MARK_END in block
assert "192.168.1.17\tdell.local dell" in block
# A file with the block should strip back to just its original body.
original = "127.0.0.1\tlocalhost\n" + block + "\n"
assert h._strip_block(original).strip() == "127.0.0.1\tlocalhost"
def test_hosts_strip_preserves_unmanaged_lines():
text = "1.2.3.4\tkeep.me\n" + h.MARK_BEGIN + "\n9.9.9.9\tgone\n" + h.MARK_END
assert h._strip_block(text).strip() == "1.2.3.4\tkeep.me"