Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions tests/services/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
""" Multicast DNS Service Discovery for Python, v0.14-wmcbrine
Copyright 2003 Paul Scott-Murphy, 2014 William McBrine

This module provides a framework for the use of DNS Service Discovery
using IP multicast.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
USA
"""
143 changes: 143 additions & 0 deletions tests/services/test_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-


"""Unit tests for zeroconf._services.types."""

import os
import unittest
import socket

import zeroconf as r
from zeroconf import Zeroconf, ServiceInfo, ZeroconfServiceTypes

from .. import _clear_cache, has_working_ipv6


class ServiceTypesQuery(unittest.TestCase):
def test_integration_with_listener(self):

type_ = "_test-srvc-type._tcp.local."
name = "xxxyyy"
registration_name = "%s.%s" % (name, type_)

zeroconf_registrar = Zeroconf(interfaces=['127.0.0.1'])
desc = {'path': '/~paulsm/'}
info = ServiceInfo(
type_,
registration_name,
80,
0,
0,
desc,
"ash-2.local.",
addresses=[socket.inet_aton("10.0.1.2")],
)
zeroconf_registrar.register_service(info)

try:
service_types = ZeroconfServiceTypes.find(interfaces=['127.0.0.1'], timeout=0.5)
assert type_ in service_types
_clear_cache(zeroconf_registrar)
service_types = ZeroconfServiceTypes.find(zc=zeroconf_registrar, timeout=0.5)
assert type_ in service_types

finally:
zeroconf_registrar.close()

@unittest.skipIf(not has_working_ipv6(), 'Requires IPv6')
@unittest.skipIf(os.environ.get('SKIP_IPV6'), 'IPv6 tests disabled')
def test_integration_with_listener_v6_records(self):

type_ = "_test-srvc-type._tcp.local."
name = "xxxyyy"
registration_name = "%s.%s" % (name, type_)
addr = "2606:2800:220:1:248:1893:25c8:1946" # example.com

zeroconf_registrar = Zeroconf(interfaces=['127.0.0.1'])
desc = {'path': '/~paulsm/'}
info = ServiceInfo(
type_,
registration_name,
80,
0,
0,
desc,
"ash-2.local.",
addresses=[socket.inet_pton(socket.AF_INET6, addr)],
)
zeroconf_registrar.register_service(info)

try:
service_types = ZeroconfServiceTypes.find(interfaces=['127.0.0.1'], timeout=0.5)
assert type_ in service_types
_clear_cache(zeroconf_registrar)
service_types = ZeroconfServiceTypes.find(zc=zeroconf_registrar, timeout=0.5)
assert type_ in service_types

finally:
zeroconf_registrar.close()

@unittest.skipIf(not has_working_ipv6(), 'Requires IPv6')
@unittest.skipIf(os.environ.get('SKIP_IPV6'), 'IPv6 tests disabled')
def test_integration_with_listener_ipv6(self):

type_ = "_test-srvc-type._tcp.local."
name = "xxxyyy"
registration_name = "%s.%s" % (name, type_)

zeroconf_registrar = Zeroconf(ip_version=r.IPVersion.V6Only)
desc = {'path': '/~paulsm/'}
info = ServiceInfo(
type_,
registration_name,
80,
0,
0,
desc,
"ash-2.local.",
addresses=[socket.inet_aton("10.0.1.2")],
)
zeroconf_registrar.register_service(info)

try:
service_types = ZeroconfServiceTypes.find(ip_version=r.IPVersion.V6Only, timeout=0.5)
assert type_ in service_types
_clear_cache(zeroconf_registrar)
service_types = ZeroconfServiceTypes.find(zc=zeroconf_registrar, timeout=0.5)
assert type_ in service_types

finally:
zeroconf_registrar.close()

def test_integration_with_subtype_and_listener(self):
subtype_ = "_subtype._sub"
type_ = "_type._tcp.local."
name = "xxxyyy"
# Note: discovery returns only DNS-SD type not subtype
discovery_type = "%s.%s" % (subtype_, type_)
registration_name = "%s.%s" % (name, type_)

zeroconf_registrar = Zeroconf(interfaces=['127.0.0.1'])
desc = {'path': '/~paulsm/'}
info = ServiceInfo(
discovery_type,
registration_name,
80,
0,
0,
desc,
"ash-2.local.",
addresses=[socket.inet_aton("10.0.1.2")],
)
zeroconf_registrar.register_service(info)

try:
service_types = ZeroconfServiceTypes.find(interfaces=['127.0.0.1'], timeout=0.5)
assert discovery_type in service_types
_clear_cache(zeroconf_registrar)
service_types = ZeroconfServiceTypes.find(zc=zeroconf_registrar, timeout=0.5)
assert discovery_type in service_types

finally:
zeroconf_registrar.close()
135 changes: 2 additions & 133 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,18 @@

import errno
import logging
import os
import socket
import time
import unittest
import unittest.mock
from threading import Event
from typing import Dict, Optional # noqa # used in type hints

import pytest

import zeroconf as r
from zeroconf import ServiceBrowser, ServiceInfo, Zeroconf, ZeroconfServiceTypes, const
from zeroconf import ServiceBrowser, ServiceInfo, Zeroconf, const

from . import has_working_ipv6, _clear_cache, _inject_response
from . import _clear_cache

log = logging.getLogger('zeroconf')
original_logging_level = logging.NOTSET
Expand Down Expand Up @@ -459,135 +457,6 @@ def test_lookups(self):
assert registry.get_types() == [type_]


class ServiceTypesQuery(unittest.TestCase):
def test_integration_with_listener(self):

type_ = "_test-srvc-type._tcp.local."
name = "xxxyyy"
registration_name = "%s.%s" % (name, type_)

zeroconf_registrar = Zeroconf(interfaces=['127.0.0.1'])
desc = {'path': '/~paulsm/'}
info = ServiceInfo(
type_,
registration_name,
80,
0,
0,
desc,
"ash-2.local.",
addresses=[socket.inet_aton("10.0.1.2")],
)
zeroconf_registrar.register_service(info)

try:
service_types = ZeroconfServiceTypes.find(interfaces=['127.0.0.1'], timeout=0.5)
assert type_ in service_types
_clear_cache(zeroconf_registrar)
service_types = ZeroconfServiceTypes.find(zc=zeroconf_registrar, timeout=0.5)
assert type_ in service_types

finally:
zeroconf_registrar.close()

@unittest.skipIf(not has_working_ipv6(), 'Requires IPv6')
@unittest.skipIf(os.environ.get('SKIP_IPV6'), 'IPv6 tests disabled')
def test_integration_with_listener_v6_records(self):

type_ = "_test-srvc-type._tcp.local."
name = "xxxyyy"
registration_name = "%s.%s" % (name, type_)
addr = "2606:2800:220:1:248:1893:25c8:1946" # example.com

zeroconf_registrar = Zeroconf(interfaces=['127.0.0.1'])
desc = {'path': '/~paulsm/'}
info = ServiceInfo(
type_,
registration_name,
80,
0,
0,
desc,
"ash-2.local.",
addresses=[socket.inet_pton(socket.AF_INET6, addr)],
)
zeroconf_registrar.register_service(info)

try:
service_types = ZeroconfServiceTypes.find(interfaces=['127.0.0.1'], timeout=0.5)
assert type_ in service_types
_clear_cache(zeroconf_registrar)
service_types = ZeroconfServiceTypes.find(zc=zeroconf_registrar, timeout=0.5)
assert type_ in service_types

finally:
zeroconf_registrar.close()

@unittest.skipIf(not has_working_ipv6(), 'Requires IPv6')
@unittest.skipIf(os.environ.get('SKIP_IPV6'), 'IPv6 tests disabled')
def test_integration_with_listener_ipv6(self):

type_ = "_test-srvc-type._tcp.local."
name = "xxxyyy"
registration_name = "%s.%s" % (name, type_)

zeroconf_registrar = Zeroconf(ip_version=r.IPVersion.V6Only)
desc = {'path': '/~paulsm/'}
info = ServiceInfo(
type_,
registration_name,
80,
0,
0,
desc,
"ash-2.local.",
addresses=[socket.inet_aton("10.0.1.2")],
)
zeroconf_registrar.register_service(info)

try:
service_types = ZeroconfServiceTypes.find(ip_version=r.IPVersion.V6Only, timeout=0.5)
assert type_ in service_types
_clear_cache(zeroconf_registrar)
service_types = ZeroconfServiceTypes.find(zc=zeroconf_registrar, timeout=0.5)
assert type_ in service_types

finally:
zeroconf_registrar.close()

def test_integration_with_subtype_and_listener(self):
subtype_ = "_subtype._sub"
type_ = "_type._tcp.local."
name = "xxxyyy"
# Note: discovery returns only DNS-SD type not subtype
discovery_type = "%s.%s" % (subtype_, type_)
registration_name = "%s.%s" % (name, type_)

zeroconf_registrar = Zeroconf(interfaces=['127.0.0.1'])
desc = {'path': '/~paulsm/'}
info = ServiceInfo(
discovery_type,
registration_name,
80,
0,
0,
desc,
"ash-2.local.",
addresses=[socket.inet_aton("10.0.1.2")],
)
zeroconf_registrar.register_service(info)

try:
service_types = ZeroconfServiceTypes.find(interfaces=['127.0.0.1'], timeout=0.5)
assert discovery_type in service_types
_clear_cache(zeroconf_registrar)
service_types = ZeroconfServiceTypes.find(zc=zeroconf_registrar, timeout=0.5)
assert discovery_type in service_types

finally:
zeroconf_registrar.close()


def test_ptr_optimization():

# instantiate a zeroconf instance
Expand Down