Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure service browser does not collapse on bad dns names #38851

Merged
merged 1 commit into from Aug 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion homeassistant/components/zeroconf/__init__.py
Expand Up @@ -8,6 +8,7 @@
from zeroconf import (
DNSPointer,
DNSRecord,
Error as ZeroconfError,
InterfaceChoice,
IPVersion,
NonUniqueNameException,
Expand Down Expand Up @@ -212,7 +213,12 @@ def service_update(zeroconf, service_type, name, state_change):
if state_change != ServiceStateChange.Added:
return

service_info = zeroconf.get_service_info(service_type, name)
try:
service_info = zeroconf.get_service_info(service_type, name)
except ZeroconfError:
_LOGGER.exception("Failed to get info for device %s", name)
return

if not service_info:
# Prevent the browser thread from collapsing as
# service_info can be None
Expand Down
22 changes: 21 additions & 1 deletion tests/components/zeroconf/test_init.py
@@ -1,5 +1,11 @@
"""Test Zeroconf component setup process."""
from zeroconf import InterfaceChoice, IPVersion, ServiceInfo, ServiceStateChange
from zeroconf import (
BadTypeInNameException,
InterfaceChoice,
IPVersion,
ServiceInfo,
ServiceStateChange,
)

from homeassistant.components import zeroconf
from homeassistant.components.zeroconf import CONF_DEFAULT_INTERFACE, CONF_IPV6
Expand Down Expand Up @@ -167,6 +173,20 @@ async def test_setup_with_ipv6_default(hass, mock_zeroconf):
assert mock_zeroconf.called_with()


async def test_service_with_invalid_name(hass, mock_zeroconf, caplog):
"""Test we do not crash on service with an invalid name."""
with patch.object(
zeroconf, "HaServiceBrowser", side_effect=service_update_mock
) as mock_service_browser:
mock_zeroconf.get_service_info.side_effect = BadTypeInNameException
assert await async_setup_component(hass, zeroconf.DOMAIN, {zeroconf.DOMAIN: {}})
hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED)
await hass.async_block_till_done()

assert len(mock_service_browser.mock_calls) == 1
assert "Failed to get info for device name" in caplog.text


async def test_homekit_match_partial_space(hass, mock_zeroconf):
"""Test configured options for a device are loaded via config entry."""
with patch.dict(
Expand Down