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
15 changes: 5 additions & 10 deletions tests/test_aio.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,12 @@

import pytest

from zeroconf import (
BadTypeInNameException,
NonUniqueNameException,
ServiceInfo,
ServiceListener,
ServiceNameAlreadyRegistered,
Zeroconf,
_LISTENER_TIME,
current_time_millis,
)
from zeroconf.aio import AsyncServiceInfo, AsyncServiceListener, AsyncZeroconf
from zeroconf.core import Zeroconf
from zeroconf.const import _LISTENER_TIME
from zeroconf.exceptions import BadTypeInNameException, NonUniqueNameException, ServiceNameAlreadyRegistered
from zeroconf.services import ServiceInfo, ServiceListener
from zeroconf.utils.time import current_time_millis


@pytest.fixture(autouse=True)
Expand Down
61 changes: 43 additions & 18 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import zeroconf as r
from zeroconf import core
from zeroconf import const

from . import has_working_ipv6, _inject_response

Expand All @@ -39,8 +40,8 @@ def test_reaper(self):
zeroconf = core.Zeroconf(interfaces=['127.0.0.1'])
cache = zeroconf.cache
original_entries = list(itertools.chain(*[cache.entries_with_name(name) for name in cache.names()]))
record_with_10s_ttl = r.DNSAddress('a', r._TYPE_SOA, r._CLASS_IN, 10, b'a')
record_with_1s_ttl = r.DNSAddress('a', r._TYPE_SOA, r._CLASS_IN, 1, b'b')
record_with_10s_ttl = r.DNSAddress('a', const._TYPE_SOA, const._CLASS_IN, 10, b'a')
record_with_1s_ttl = r.DNSAddress('a', const._TYPE_SOA, const._CLASS_IN, 1, b'b')
zeroconf.cache.add(record_with_10s_ttl)
zeroconf.cache.add(record_with_1s_ttl)
entries_with_cache = list(itertools.chain(*[cache.entries_with_name(name) for name in cache.names()]))
Expand Down Expand Up @@ -102,34 +103,51 @@ def test_launch_and_close_v6_only(self):
def test_handle_response(self):
def mock_incoming_msg(service_state_change: r.ServiceStateChange) -> r.DNSIncoming:
ttl = 120
generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE)
generated = r.DNSOutgoing(const._FLAGS_QR_RESPONSE)

if service_state_change == r.ServiceStateChange.Updated:
generated.add_answer_at_time(
r.DNSText(service_name, r._TYPE_TXT, r._CLASS_IN | r._CLASS_UNIQUE, ttl, service_text), 0
r.DNSText(
service_name,
const._TYPE_TXT,
const._CLASS_IN | const._CLASS_UNIQUE,
ttl,
service_text,
),
0,
)
return r.DNSIncoming(generated.packet())

if service_state_change == r.ServiceStateChange.Removed:
ttl = 0

generated.add_answer_at_time(
r.DNSPointer(service_type, r._TYPE_PTR, r._CLASS_IN, ttl, service_name), 0
r.DNSPointer(service_type, const._TYPE_PTR, const._CLASS_IN, ttl, service_name), 0
)
generated.add_answer_at_time(
r.DNSService(
service_name, r._TYPE_SRV, r._CLASS_IN | r._CLASS_UNIQUE, ttl, 0, 0, 80, service_server
service_name,
const._TYPE_SRV,
const._CLASS_IN | const._CLASS_UNIQUE,
ttl,
0,
0,
80,
service_server,
),
0,
)
generated.add_answer_at_time(
r.DNSText(service_name, r._TYPE_TXT, r._CLASS_IN | r._CLASS_UNIQUE, ttl, service_text), 0
r.DNSText(
service_name, const._TYPE_TXT, const._CLASS_IN | const._CLASS_UNIQUE, ttl, service_text
),
0,
)
generated.add_answer_at_time(
r.DNSAddress(
service_server,
r._TYPE_A,
r._CLASS_IN | r._CLASS_UNIQUE,
const._TYPE_A,
const._CLASS_IN | const._CLASS_UNIQUE,
ttl,
socket.inet_aton(service_address),
),
Expand All @@ -141,20 +159,27 @@ def mock_incoming_msg(service_state_change: r.ServiceStateChange) -> r.DNSIncomi
def mock_split_incoming_msg(service_state_change: r.ServiceStateChange) -> r.DNSIncoming:
"""Mock an incoming message for the case where the packet is split."""
ttl = 120
generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE)
generated = r.DNSOutgoing(const._FLAGS_QR_RESPONSE)
generated.add_answer_at_time(
r.DNSAddress(
service_server,
r._TYPE_A,
r._CLASS_IN | r._CLASS_UNIQUE,
const._TYPE_A,
const._CLASS_IN | const._CLASS_UNIQUE,
ttl,
socket.inet_aton(service_address),
),
0,
)
generated.add_answer_at_time(
r.DNSService(
service_name, r._TYPE_SRV, r._CLASS_IN | r._CLASS_UNIQUE, ttl, 0, 0, 80, service_server
service_name,
const._TYPE_SRV,
const._CLASS_IN | const._CLASS_UNIQUE,
ttl,
0,
0,
80,
service_server,
),
0,
)
Expand All @@ -171,10 +196,10 @@ def mock_split_incoming_msg(service_state_change: r.ServiceStateChange) -> r.DNS
try:
# service added
_inject_response(zeroconf, mock_incoming_msg(r.ServiceStateChange.Added))
dns_text = zeroconf.cache.get_by_details(service_name, r._TYPE_TXT, r._CLASS_IN)
dns_text = zeroconf.cache.get_by_details(service_name, const._TYPE_TXT, const._CLASS_IN)
assert dns_text is not None
assert cast(r.DNSText, dns_text).text == service_text # service_text is b'path=/~paulsm/'
all_dns_text = zeroconf.cache.get_all_by_details(service_name, r._TYPE_TXT, r._CLASS_IN)
all_dns_text = zeroconf.cache.get_all_by_details(service_name, const._TYPE_TXT, const._CLASS_IN)
assert [dns_text] == all_dns_text

# https://tools.ietf.org/html/rfc6762#section-10.2
Expand All @@ -188,7 +213,7 @@ def mock_split_incoming_msg(service_state_change: r.ServiceStateChange) -> r.DNS
# service updated. currently only text record can be updated
service_text = b'path=/~humingchun/'
_inject_response(zeroconf, mock_incoming_msg(r.ServiceStateChange.Updated))
dns_text = zeroconf.cache.get_by_details(service_name, r._TYPE_TXT, r._CLASS_IN)
dns_text = zeroconf.cache.get_by_details(service_name, const._TYPE_TXT, const._CLASS_IN)
assert dns_text is not None
assert cast(r.DNSText, dns_text).text == service_text # service_text is b'path=/~humingchun/'

Expand All @@ -198,13 +223,13 @@ def mock_split_incoming_msg(service_state_change: r.ServiceStateChange) -> r.DNS
# This should not evict TXT records from the cache
_inject_response(zeroconf, mock_split_incoming_msg(r.ServiceStateChange.Updated))
time.sleep(1.1)
dns_text = zeroconf.cache.get_by_details(service_name, r._TYPE_TXT, r._CLASS_IN)
dns_text = zeroconf.cache.get_by_details(service_name, const._TYPE_TXT, const._CLASS_IN)
assert dns_text is not None
assert cast(r.DNSText, dns_text).text == service_text # service_text is b'path=/~humingchun/'

# service removed
_inject_response(zeroconf, mock_incoming_msg(r.ServiceStateChange.Removed))
dns_text = zeroconf.cache.get_by_details(service_name, r._TYPE_TXT, r._CLASS_IN)
dns_text = zeroconf.cache.get_by_details(service_name, const._TYPE_TXT, const._CLASS_IN)
assert dns_text is None

finally:
Expand Down
Loading