Skip to content

Commit 4ea3c5f

Browse files
committed
Permit the ServiceBrowser to browse overlong types
- At least one type "tivo-videostream" exists in the wild so we are permissive about what we will look for, and strict about what we will announce.
1 parent 5752ace commit 4ea3c5f

3 files changed

Lines changed: 36 additions & 1 deletion

File tree

tests/test_services.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,3 +1258,20 @@ def test_changing_name_updates_serviceinfo_key():
12581258
assert info_service.key == "mytesthome._homeassistant._tcp.local."
12591259
info_service.name = "YourTestHome._homeassistant._tcp.local."
12601260
assert info_service.key == "yourtesthome._homeassistant._tcp.local."
1261+
1262+
1263+
def test_servicebrowser_uses_non_strict_names():
1264+
"""Verify we can look for technically invalid names as we cannot change what others do."""
1265+
1266+
# dummy service callback
1267+
def on_service_state_change(zeroconf, service_type, state_change, name):
1268+
pass
1269+
1270+
zc = r.Zeroconf(interfaces=['127.0.0.1'])
1271+
browser = ServiceBrowser(zc, ["_tivo-videostream._tcp.local."], [on_service_state_change])
1272+
browser.cancel()
1273+
1274+
# Still fail on completely invalid
1275+
with pytest.raises(r.BadTypeInNameException):
1276+
browser = ServiceBrowser(zc, ["tivo-videostream._tcp.local."], [on_service_state_change])
1277+
zc.close()

tests/utils/test_name.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
5+
"""Unit tests for zeroconf._utils.name."""
6+
7+
import pytest
8+
9+
from zeroconf._utils import name as nameutils
10+
from zeroconf import BadTypeInNameException
11+
12+
13+
def test_service_type_name():
14+
"""Test overlong service_type_name."""
15+
with pytest.raises(BadTypeInNameException):
16+
nameutils.service_type_name("Tivo1._tivo-videostream._tcp.local.")
17+
nameutils.service_type_name("Tivo1._tivo-videostream._tcp.local.", strict=False)

zeroconf/_utils/name.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ def service_type_name(type_: str, *, strict: bool = True) -> str: # pylint: dis
104104

105105
test_service_name = service_name[1:]
106106

107-
if len(test_service_name) > 15:
107+
if strict and len(test_service_name) > 15:
108+
# https://datatracker.ietf.org/doc/html/rfc6763#section-7.2
108109
raise BadTypeInNameException("Service name (%s) must be <= 15 bytes" % test_service_name)
109110

110111
if '--' in test_service_name:

0 commit comments

Comments
 (0)