Compare commits

...
12 changed files with 124 additions and 106 deletions

No files matched your search

+33 -6
View File
@@ -4,7 +4,12 @@ from __future__ import annotations
import asyncio
from collections.abc import AsyncGenerator, Callable, Generator
from contextlib import AbstractAsyncContextManager, asynccontextmanager, suppress
from contextlib import (
AbstractAsyncContextManager,
asynccontextmanager,
contextmanager,
suppress,
)
import fcntl
from functools import cache
import hashlib
@@ -179,19 +184,36 @@ def integration_test_dir() -> Generator[Path]:
yield Path(tmpdir)
@pytest.fixture
def reserved_tcp_port() -> Generator[tuple[int, socket.socket]]:
"""Reserve an unused TCP port by holding the socket open."""
@contextmanager
def _reserve_port() -> Generator[tuple[int, socket.socket]]:
"""Hold an unused TCP port open until the caller is done with it."""
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(("", 0))
port = s.getsockname()[1]
try:
yield port, s
yield s.getsockname()[1], s
finally:
s.close()
@pytest.fixture
def reserved_tcp_port() -> Generator[tuple[int, socket.socket]]:
"""Reserve an unused TCP port by holding the socket open."""
with _reserve_port() as reserved:
yield reserved
@pytest.fixture
def reserved_ota_port() -> Generator[tuple[int, socket.socket]]:
"""Reserve a second port, for fixtures that name their OTA port __OTA_PORT__.
Only the api socket is closed by run_compiled, so a test using this one closes its
socket itself before starting the binary, or the device cannot bind the port.
"""
with _reserve_port() as reserved:
yield reserved
@pytest.fixture
def unused_tcp_port(reserved_tcp_port: tuple[int, socket.socket]) -> int:
"""Get the reserved TCP port number."""
@@ -227,6 +249,11 @@ async def yaml_config(request: pytest.FixtureRequest, unused_tcp_port: int) -> s
# Add port configuration after api:
content = content.replace("api:", f"api:\n port: {unused_tcp_port}")
# Filled here rather than by the test, so tests sharing an OTA fixture can share its build
if "__OTA_PORT__" in content:
ota_port, _ = request.getfixturevalue("reserved_ota_port")
content = content.replace("__OTA_PORT__", str(ota_port))
# Add debug build flags for integration tests to enable assertions
if "esphome:" in content and "build_flags:" not in content:
# Add build_flags with debug flags after esphome:
@@ -1,6 +0,0 @@
esphome:
name: zero-psk-plaintext-test
host:
api:
encryption:
logger:
@@ -1,9 +0,0 @@
esphome:
name: host-ota-test
host:
api:
ota:
- platform: esphome
port: __OTA_PORT__
logger:
level: DEBUG
@@ -1,9 +0,0 @@
esphome:
name: host-ota-test
host:
api:
ota:
- platform: esphome
port: __OTA_PORT__
logger:
level: DEBUG
@@ -1,9 +0,0 @@
esphome:
name: host-ota-test
host:
api:
ota:
- platform: esphome
port: __OTA_PORT__
logger:
level: DEBUG
@@ -1,11 +0,0 @@
esphome:
name: oversized-noise
host:
api:
encryption:
key: N4Yle5YirwZhPiHHsdZLdOA73ndj/84veVaLhTvxCuU=
logger:
level: VERY_VERBOSE
@@ -1,11 +0,0 @@
esphome:
name: oversized-noise
host:
api:
encryption:
key: N4Yle5YirwZhPiHHsdZLdOA73ndj/84veVaLhTvxCuU=
logger:
level: VERY_VERBOSE
@@ -1,9 +0,0 @@
esphome:
name: oversized-protobuf-plaintext
host:
api:
logger:
level: VERY_VERBOSE
@@ -154,6 +154,7 @@ async def light_entity(
@pytest.mark.asyncio
@pytest.mark.shared_yaml("api_homeassistant_light")
async def test_api_homeassistant_light(
yaml_config: str,
run_compiled: RunCompiledFunction,
@@ -29,6 +29,7 @@ NEW_KEY = PROVISIONING_PSK
@pytest.mark.asyncio
@pytest.mark.shared_yaml("api_zero_psk_provisioning")
async def test_api_zero_psk_provisioning(
yaml_config: str,
write_yaml_config: ConfigWriter,
@@ -116,6 +117,7 @@ async def test_api_zero_psk_provisioning(
@pytest.mark.asyncio
@pytest.mark.shared_yaml("api_zero_psk_provisioning")
async def test_api_zero_psk_provisioning_plaintext(
yaml_config: str,
run_compiled: RunCompiledFunction,
@@ -125,7 +127,7 @@ async def test_api_zero_psk_provisioning_plaintext(
log_lines: list[str] = []
async with run_compiled(yaml_config, line_callback=log_lines.append):
async with api_client_connected() as client:
await assert_device_name(client, "zero-psk-plaintext-test")
await assert_device_name(client, "zero-psk-provision-test")
assert await client.noise_encryption_set_key(NEW_KEY) is True
@@ -136,7 +138,7 @@ async def test_api_zero_psk_provisioning_plaintext(
# The new key works; the zero PSK does not
async with api_client_connected(noise_psk=NEW_KEY.decode()) as client:
await assert_device_name(client, "zero-psk-plaintext-test")
await assert_device_name(client, "zero-psk-provision-test")
with pytest.raises(InvalidEncryptionKeyAPIError):
async with api_client_connected(noise_psk=ZERO_PSK, timeout=5) as client:
+79 -32
View File
@@ -103,17 +103,6 @@ def _udp_mode(
yield used
@contextmanager
def _reserve_port() -> Generator[tuple[int, socket.socket]]:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(("", 0))
try:
yield s.getsockname()[1], s
finally:
s.close()
async def _wait_for_line(lines: list[str], needle: str, timeout: float = 5.0) -> None:
"""The config dump prints after every setup, a little after the api port
opens, so wait for it rather than assert on the lines seen so far."""
@@ -143,18 +132,17 @@ async def _build(
write_yaml_config: ConfigWriter,
compile_esphome: CompileFunction,
reserved_tcp_port: tuple[int, socket.socket],
reserved_ota_port: tuple[int, socket.socket],
) -> tuple[int, int, Path, Path]:
"""Reserve an OTA port, compile the fixture with it, and release both
ports right before the binary is started. Returns the stripped binary to
run and upload, then the unstripped build for crash backtraces."""
"""Compile the fixture, which conftest already gave the reserved OTA port, and
release both ports right before the binary is started. Returns the stripped
binary to run and upload, then the unstripped build for crash backtraces."""
api_port, api_socket = reserved_tcp_port
with _reserve_port() as (ota_port, ota_socket):
config_path = await write_yaml_config(
yaml_config.replace("__OTA_PORT__", str(ota_port))
)
binary_path = await compile_esphome(config_path)
api_socket.close()
ota_socket.close()
ota_port, ota_socket = reserved_ota_port
config_path = await write_yaml_config(yaml_config)
binary_path = await compile_esphome(config_path)
api_socket.close()
ota_socket.close()
return api_port, ota_port, await _strip_debug(binary_path), binary_path
@@ -294,16 +282,22 @@ async def _provision_key(
@pytest.mark.asyncio
@pytest.mark.shared_yaml("host_ota_self_update")
async def test_host_ota_self_update(
yaml_config: str,
write_yaml_config: ConfigWriter,
compile_esphome: CompileFunction,
reserved_tcp_port: tuple[int, socket.socket],
reserved_ota_port: tuple[int, socket.socket],
) -> None:
"""Self-OTA: upload the running binary back to itself, expect re-exec."""
dev = _Device(
*await _build(
yaml_config, write_yaml_config, compile_esphome, reserved_tcp_port
yaml_config,
write_yaml_config,
compile_esphome,
reserved_tcp_port,
reserved_ota_port,
)
)
async with run_binary(
@@ -328,17 +322,23 @@ async def test_host_ota_self_update(
@pytest.mark.asyncio
@pytest.mark.shared_yaml("host_ota_self_update")
async def test_host_ota_deflate_rtl8710b_bit(
yaml_config: str,
write_yaml_config: ConfigWriter,
compile_esphome: CompileFunction,
reserved_tcp_port: tuple[int, socket.socket],
reserved_ota_port: tuple[int, socket.socket],
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""The device inflates an upload asked for with the RTL8710B deflate bit."""
dev = _Device(
*await _build(
yaml_config, write_yaml_config, compile_esphome, reserved_tcp_port
yaml_config,
write_yaml_config,
compile_esphome,
reserved_tcp_port,
reserved_ota_port,
)
)
monkeypatch.setattr(espota2, "_is_rtl8710b_image", lambda image: True)
@@ -354,18 +354,24 @@ async def test_host_ota_deflate_rtl8710b_bit(
@pytest.mark.asyncio
@pytest.mark.shared_yaml("host_ota_self_update")
async def test_host_ota_deflate(
yaml_config: str,
write_yaml_config: ConfigWriter,
compile_esphome: CompileFunction,
reserved_tcp_port: tuple[int, socket.socket],
reserved_ota_port: tuple[int, socket.socket],
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Deflate is negotiated by default, an old client gets an uncompressed
upload, and a corrupt stream is rejected without taking the device down."""
dev = _Device(
*await _build(
yaml_config, write_yaml_config, compile_esphome, reserved_tcp_port
yaml_config,
write_yaml_config,
compile_esphome,
reserved_tcp_port,
reserved_ota_port,
)
)
@@ -445,6 +451,7 @@ async def test_host_ota_encrypted(
write_yaml_config: ConfigWriter,
compile_esphome: CompileFunction,
reserved_tcp_port: tuple[int, socket.socket],
reserved_ota_port: tuple[int, socket.socket],
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""A client that leaves right after the handshake, as a key probe does,
@@ -453,7 +460,11 @@ async def test_host_ota_encrypted(
pytest.importorskip("aioesphomeapi.noise")
dev = _Device(
*await _build(
yaml_config, write_yaml_config, compile_esphome, reserved_tcp_port
yaml_config,
write_yaml_config,
compile_esphome,
reserved_tcp_port,
reserved_ota_port,
)
)
async with run_binary(
@@ -483,13 +494,18 @@ async def test_host_ota_encrypted_safe_mode(
write_yaml_config: ConfigWriter,
compile_esphome: CompileFunction,
reserved_tcp_port: tuple[int, socket.socket],
reserved_ota_port: tuple[int, socket.socket],
) -> None:
"""Safe mode never constructs the api server, so an encrypted OTA with the
api key has to run on the ota component's own copy of that key."""
pytest.importorskip("aioesphomeapi.noise")
dev = _Device(
*await _build(
yaml_config, write_yaml_config, compile_esphome, reserved_tcp_port
yaml_config,
write_yaml_config,
compile_esphome,
reserved_tcp_port,
reserved_ota_port,
)
)
# The api port never opens in safe mode, so wait for the log line instead
@@ -510,6 +526,7 @@ async def test_host_ota_api_key_offer_with_password(
write_yaml_config: ConfigWriter,
compile_esphome: CompileFunction,
reserved_tcp_port: tuple[int, socket.socket],
reserved_ota_port: tuple[int, socket.socket],
caplog: pytest.LogCaptureFixture,
) -> None:
"""With only an api key the device offers encryption without requiring
@@ -520,7 +537,11 @@ async def test_host_ota_api_key_offer_with_password(
wrong_key = base64.b64encode(b"w" * 32).decode()
dev = _Device(
*await _build(
yaml_config, write_yaml_config, compile_esphome, reserved_tcp_port
yaml_config,
write_yaml_config,
compile_esphome,
reserved_tcp_port,
reserved_ota_port,
)
)
async with run_binary(
@@ -562,12 +583,17 @@ async def test_host_ota_cleared_password(
write_yaml_config: ConfigWriter,
compile_esphome: CompileFunction,
reserved_tcp_port: tuple[int, socket.socket],
reserved_ota_port: tuple[int, socket.socket],
) -> None:
"""A password cleared at boot with set_auth_password("") must not be
asked for: the auth path is compiled in but the device skips it."""
dev = _Device(
*await _build(
yaml_config, write_yaml_config, compile_esphome, reserved_tcp_port
yaml_config,
write_yaml_config,
compile_esphome,
reserved_tcp_port,
reserved_ota_port,
)
)
async with run_binary(
@@ -588,6 +614,7 @@ async def test_host_ota_provisioned_api_key(
write_yaml_config: ConfigWriter,
compile_esphome: CompileFunction,
reserved_tcp_port: tuple[int, socket.socket],
reserved_ota_port: tuple[int, socket.socket],
api_client_connected: APIClientConnectedFactory,
) -> None:
"""A key provisioned over the api feeds the OTA offer: plaintext works
@@ -597,7 +624,11 @@ async def test_host_ota_provisioned_api_key(
pytest.importorskip("aioesphomeapi.noise")
dev = _Device(
*await _build(
yaml_config, write_yaml_config, compile_esphome, reserved_tcp_port
yaml_config,
write_yaml_config,
compile_esphome,
reserved_tcp_port,
reserved_ota_port,
)
)
async with run_binary(
@@ -627,6 +658,7 @@ async def test_host_ota_provisioned_api_key_safe_mode(
write_yaml_config: ConfigWriter,
compile_esphome: CompileFunction,
reserved_tcp_port: tuple[int, socket.socket],
reserved_ota_port: tuple[int, socket.socket],
api_client_connected: APIClientConnectedFactory,
) -> None:
"""Safe mode never constructs the api server, so the OTA has to load the
@@ -634,7 +666,11 @@ async def test_host_ota_provisioned_api_key_safe_mode(
pytest.importorskip("aioesphomeapi.noise")
dev = _Device(
*await _build(
yaml_config, write_yaml_config, compile_esphome, reserved_tcp_port
yaml_config,
write_yaml_config,
compile_esphome,
reserved_tcp_port,
reserved_ota_port,
)
)
async with run_binary(
@@ -665,17 +701,23 @@ async def test_host_ota_provisioned_api_key_safe_mode(
@pytest.mark.asyncio
@pytest.mark.shared_yaml("host_ota_self_update")
async def test_host_ota_rejects_garbage(
yaml_config: str,
write_yaml_config: ConfigWriter,
compile_esphome: CompileFunction,
reserved_tcp_port: tuple[int, socket.socket],
reserved_ota_port: tuple[int, socket.socket],
integration_test_dir,
) -> None:
"""Bogus payload is rejected and the device keeps running."""
dev = _Device(
*await _build(
yaml_config, write_yaml_config, compile_esphome, reserved_tcp_port
yaml_config,
write_yaml_config,
compile_esphome,
reserved_tcp_port,
reserved_ota_port,
)
)
# 192 bytes that are neither ELF nor Mach-O.
@@ -733,6 +775,7 @@ async def test_host_ota_automations(
write_yaml_config: ConfigWriter,
compile_esphome: CompileFunction,
reserved_tcp_port: tuple[int, socket.socket],
reserved_ota_port: tuple[int, socket.socket],
integration_test_dir: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
@@ -740,7 +783,11 @@ async def test_host_ota_automations(
automation on one key fires."""
dev = _Device(
*await _build(
yaml_config, write_yaml_config, compile_esphome, reserved_tcp_port
yaml_config,
write_yaml_config,
compile_esphome,
reserved_tcp_port,
reserved_ota_port,
)
)
bogus_path = integration_test_dir / "bogus.bin"
+7 -2
View File
@@ -20,6 +20,7 @@ def _crashed(waiter: LineWaiter) -> bool:
@pytest.mark.asyncio
@pytest.mark.shared_yaml("oversized_payload_plaintext")
async def test_oversized_payload_plaintext(
yaml_config: str,
run_compiled: RunCompiledFunction,
@@ -61,6 +62,7 @@ async def test_oversized_payload_plaintext(
@pytest.mark.asyncio
@pytest.mark.shared_yaml("oversized_payload_plaintext")
async def test_oversized_protobuf_message_id_plaintext(
yaml_config: str,
run_compiled: RunCompiledFunction,
@@ -75,7 +77,7 @@ async def test_oversized_protobuf_message_id_plaintext(
async with run_compiled(yaml_config, line_callback=waiter.callback):
async with api_client_connected_with_disconnect() as (client, disconnect_event):
# Verify basic connection works first
await assert_device_name(client, "oversized-protobuf-plaintext")
await assert_device_name(client, "oversized-plaintext")
# Access the internal connection to send raw message with large ID
frame_helper = client._connection._frame_helper
@@ -101,10 +103,11 @@ async def test_oversized_protobuf_message_id_plaintext(
# Try to reconnect to verify the process is still running
async with api_client_connected_with_disconnect() as (client2, _):
await assert_device_name(client2, "oversized-protobuf-plaintext")
await assert_device_name(client2, "oversized-plaintext")
@pytest.mark.asyncio
@pytest.mark.shared_yaml("oversized_payload_noise")
async def test_oversized_payload_noise(
yaml_config: str,
run_compiled: RunCompiledFunction,
@@ -154,6 +157,7 @@ async def test_oversized_payload_noise(
@pytest.mark.asyncio
@pytest.mark.shared_yaml("oversized_payload_noise")
async def test_oversized_protobuf_message_id_noise(
yaml_config: str,
run_compiled: RunCompiledFunction,
@@ -211,6 +215,7 @@ async def test_oversized_protobuf_message_id_noise(
@pytest.mark.asyncio
@pytest.mark.shared_yaml("oversized_payload_noise")
async def test_noise_corrupt_encrypted_frame(
yaml_config: str,
run_compiled: RunCompiledFunction,