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
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 113 additions and 7 deletions

View file

@ -10,6 +10,7 @@ from homeassistant import core
from homeassistant.helpers.json import (
ExtendedJSONEncoder,
JSONEncoder,
json_bytes_strip_null,
json_dumps,
json_dumps_sorted,
)
@ -118,3 +119,19 @@ def test_json_dumps_rgb_color_subclass():
rgb = RGBColor(4, 2, 1)
assert json_dumps(rgb) == "[4,2,1]"
def test_json_bytes_strip_null():
"""Test stripping nul from strings."""
assert json_bytes_strip_null("\0") == b'""'
assert json_bytes_strip_null("silly\0stuff") == b'"silly"'
assert json_bytes_strip_null(["one", "two\0", "three"]) == b'["one","two","three"]'
assert (
json_bytes_strip_null({"k1": "one", "k2": "two\0", "k3": "three"})
== b'{"k1":"one","k2":"two","k3":"three"}'
)
assert (
json_bytes_strip_null([[{"k1": {"k2": ["silly\0stuff"]}}]])
== b'[[{"k1":{"k2":["silly"]}}]]'
)