Skip to content

Commit

Permalink
test: add more coverage for services with non-strict names
Browse files Browse the repository at this point in the history
  • Loading branch information
azogue committed Aug 12, 2023
1 parent 5aad0e5 commit c8996e6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
5 changes: 5 additions & 0 deletions tests/test_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,7 @@ async def test_async_service_registration_name_does_not_match_type() -> None:
@pytest.mark.asyncio
async def test_async_service_registration_name_strict_check() -> None:
"""Test registering services throws when the name does not comply."""
zc = Zeroconf(interfaces=['127.0.0.1'])
aiozc = AsyncZeroconf(interfaces=['127.0.0.1'])
type_ = "_ibisip_http._tcp.local."
name = "CustomerInformationService-F4D4895E9EEB"
Expand All @@ -475,10 +476,14 @@ async def test_async_service_registration_name_strict_check() -> None:
"ash-2.local.",
addresses=[socket.inet_aton("10.0.1.2")],
)
with pytest.raises(BadTypeInNameException):
await zc.async_check_service(info, allow_name_change=False)

with pytest.raises(BadTypeInNameException):
task = await aiozc.async_register_service(info)
await task

await zc.async_check_service(info, allow_name_change=False, strict=False)
task = await aiozc.async_register_service(info, strict=False)
await task

Expand Down
29 changes: 29 additions & 0 deletions tests/utils/test_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@


"""Unit tests for zeroconf._utils.name."""
import socket

import pytest

from zeroconf import BadTypeInNameException
from zeroconf._services.info import ServiceInfo, instance_name_from_service_info
from zeroconf._utils import name as nameutils


Expand All @@ -25,6 +27,33 @@ def test_service_type_name_overlong_full_name():
nameutils.service_type_name(f"{long_name}._tivo-videostream._tcp.local.", strict=False)


@pytest.mark.parametrize(
"instance_name, service_type",
(
("CustomerInformationService-F4D4885E9EEB", "_ibisip_http._tcp.local."),
("DeviceManagementService_F4D4885E9EEB", "_ibisip_http._tcp.local."),
),
)
def test_service_type_name_non_strict_compliant_names(instance_name, service_type):
"""Test service_type_name for valid names, but not strict-compliant."""
desc = {'path': '/~paulsm/'}
service_name = f'{instance_name}.{service_type}'
service_server = 'ash-1.local.'
service_address = socket.inet_aton("10.0.1.2")
info = ServiceInfo(
service_type, service_name, 22, 0, 0, desc, service_server, addresses=[service_address]
)
assert info.get_name() == instance_name

with pytest.raises(BadTypeInNameException):
nameutils.service_type_name(service_name)
with pytest.raises(BadTypeInNameException):
instance_name_from_service_info(info)

nameutils.service_type_name(service_name, strict=False)
assert instance_name_from_service_info(info, strict=False) == instance_name


def test_possible_types():
"""Test possible types from name."""
assert nameutils.possible_types('.') == set()
Expand Down

0 comments on commit c8996e6

Please sign in to comment.