1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 12:59:34 +00:00

Terminate strings at NUL when recording states and events (#86687)

This commit is contained in:
Erik Montnemery
2023-01-26 11:11:03 +01:00
committed by GitHub
parent b9ffc67a44
commit fea30c1ce9
6 changed files with 113 additions and 7 deletions

View File

@@ -31,6 +31,7 @@ from homeassistant.components.recorder.const import (
EVENT_RECORDER_5MIN_STATISTICS_GENERATED,
EVENT_RECORDER_HOURLY_STATISTICS_GENERATED,
KEEPALIVE_TIME,
SupportedDialect,
)
from homeassistant.components.recorder.db_schema import (
SCHEMA_VERSION,
@@ -223,6 +224,42 @@ async def test_saving_state(recorder_mock, hass: HomeAssistant):
assert state == _state_with_context(hass, entity_id)
@pytest.mark.parametrize(
"dialect_name, expected_attributes",
(
(SupportedDialect.MYSQL, {"test_attr": 5, "test_attr_10": "silly\0stuff"}),
(SupportedDialect.POSTGRESQL, {"test_attr": 5, "test_attr_10": "silly"}),
(SupportedDialect.SQLITE, {"test_attr": 5, "test_attr_10": "silly\0stuff"}),
),
)
async def test_saving_state_with_nul(
recorder_mock, hass: HomeAssistant, dialect_name, expected_attributes
):
"""Test saving and restoring a state with nul in attributes."""
entity_id = "test.recorder"
state = "restoring_from_db"
attributes = {"test_attr": 5, "test_attr_10": "silly\0stuff"}
with patch(
"homeassistant.components.recorder.core.Recorder.dialect_name", dialect_name
):
hass.states.async_set(entity_id, state, attributes)
await async_wait_recording_done(hass)
with session_scope(hass=hass) as session:
db_states = []
for db_state, db_state_attributes in session.query(States, StateAttributes):
db_states.append(db_state)
state = db_state.to_native()
state.attributes = db_state_attributes.to_native()
assert len(db_states) == 1
assert db_states[0].event_id is None
expected = _state_with_context(hass, entity_id)
expected.attributes = expected_attributes
assert state == expected
async def test_saving_many_states(
async_setup_recorder_instance: SetupRecorderInstanceT, hass: HomeAssistant
):