test: unit tests for licensing, LAN IP, TLS identity and rediscovery
- local_backend/tests (pytest, 48 tests, <1s): license token verify/evaluate (tamper, other site/key, offline, grace, workday deferral, clock, transition), netinfo detection on fake sysfs (VPN/tunnel/WiFi/link-local), LAN-IP resolver priority + mismatch + staleness + override validation, TLS identity create/restart/renew-same-key/foreign cert (+ openssl pin check) - waiter_pwa/tests (node --test, 7 tests): subnet candidates, scan picks only this venue, early stop, progress - requirements-dev.txt (pytest), npm test, .pytest_cache ignored Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
"""The server's self-managed TLS identity (services/tls_identity.py, plan step 7)."""
|
||||
import shutil
|
||||
import subprocess
|
||||
from datetime import datetime, timedelta, timezone
|
||||
|
||||
import pytest
|
||||
from cryptography import x509
|
||||
from cryptography.hazmat.primitives import serialization
|
||||
from cryptography.hazmat.primitives.asymmetric import ec
|
||||
|
||||
from services.tls_identity import _issue_cert, ensure_tls_identity, spki_sha256
|
||||
|
||||
NOW = datetime.now(timezone.utc)
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def host_ip(monkeypatch):
|
||||
monkeypatch.setenv("HOST_IP", "192.168.1.50")
|
||||
|
||||
|
||||
def cert_of(d):
|
||||
return x509.load_pem_x509_certificate((d / "cert.pem").read_bytes())
|
||||
|
||||
|
||||
def test_first_run_creates_ec_key_and_10y_cert(tmp_path):
|
||||
r = ensure_tls_identity(tmp_path, NOW)
|
||||
assert r["created"]
|
||||
assert isinstance(serialization.load_pem_private_key((tmp_path / "key.pem").read_bytes(), None), ec.EllipticCurvePrivateKey)
|
||||
assert round((cert_of(tmp_path).not_valid_after_utc - NOW).days / 365) == 10
|
||||
sans = [str(n.value) for n in cert_of(tmp_path).extensions.get_extension_for_class(x509.SubjectAlternativeName).value]
|
||||
assert "192.168.1.50" in sans
|
||||
|
||||
|
||||
@pytest.mark.skipif(not shutil.which("openssl"), reason="openssl CLI not available")
|
||||
def test_pin_matches_openssl_spki(tmp_path):
|
||||
pin = ensure_tls_identity(tmp_path, NOW)["spki_sha256"]
|
||||
cmd = ("openssl x509 -in cert.pem -pubkey -noout | openssl pkey -pubin -outform der"
|
||||
" | openssl dgst -sha256 -binary | openssl base64 -A")
|
||||
assert subprocess.run(cmd, shell=True, cwd=tmp_path, capture_output=True, text=True).stdout.strip() == pin
|
||||
|
||||
|
||||
def test_restart_changes_nothing(tmp_path):
|
||||
first = ensure_tls_identity(tmp_path, NOW)
|
||||
again = ensure_tls_identity(tmp_path, NOW)
|
||||
assert (again["created"], again["renewed"], again["spki_sha256"]) == (False, False, first["spki_sha256"])
|
||||
|
||||
|
||||
def test_renewal_keeps_the_key(tmp_path):
|
||||
first = ensure_tls_identity(tmp_path, NOW)
|
||||
serial = cert_of(tmp_path).serial_number
|
||||
later = ensure_tls_identity(tmp_path, NOW + timedelta(days=365 * 8 + 30)) # < 2 years left
|
||||
assert later["renewed"] and later["spki_sha256"] == first["spki_sha256"]
|
||||
assert cert_of(tmp_path).serial_number != serial
|
||||
|
||||
|
||||
def test_foreign_cert_replaced(tmp_path):
|
||||
first = ensure_tls_identity(tmp_path, NOW)
|
||||
other = ec.generate_private_key(ec.SECP256R1())
|
||||
(tmp_path / "cert.pem").write_bytes(_issue_cert(other, None, NOW).public_bytes(serialization.Encoding.PEM))
|
||||
again = ensure_tls_identity(tmp_path, NOW)
|
||||
assert spki_sha256(cert_of(tmp_path).public_key()) == first["spki_sha256"] == again["spki_sha256"]
|
||||
Reference in New Issue
Block a user