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
44 changes: 43 additions & 1 deletion tests/test_dns.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from typing import Dict, cast # noqa # used in type hints

import zeroconf as r
from zeroconf import const
from zeroconf import const, current_time_millis
from zeroconf import (
DNSHinfo,
DNSText,
Expand Down Expand Up @@ -175,6 +175,48 @@ def test_parse_own_packet_response(self):
assert len(generated.answers) == 1
assert len(generated.answers) == len(parsed.answers)

def test_adding_empty_answer(self):
generated = r.DNSOutgoing(const._FLAGS_QR_RESPONSE)
generated.add_answer_at_time(
None,
0,
)
generated.add_answer_at_time(
r.DNSService(
"æøå.local.",
const._TYPE_SRV,
const._CLASS_IN | const._CLASS_UNIQUE,
const._DNS_HOST_TTL,
0,
0,
80,
"foo.local.",
),
0,
)
parsed = r.DNSIncoming(generated.packets()[0])
assert len(generated.answers) == 1
assert len(generated.answers) == len(parsed.answers)

def test_adding_expired_answer(self):
generated = r.DNSOutgoing(const._FLAGS_QR_RESPONSE)
generated.add_answer_at_time(
r.DNSService(
"æøå.local.",
const._TYPE_SRV,
const._CLASS_IN | const._CLASS_UNIQUE,
const._DNS_HOST_TTL,
0,
0,
80,
"foo.local.",
),
current_time_millis() + 1000000,
)
parsed = r.DNSIncoming(generated.packets()[0])
assert len(generated.answers) == 0
assert len(generated.answers) == len(parsed.answers)

def test_match_question(self):
generated = r.DNSOutgoing(const._FLAGS_QR_QUERY)
question = r.DNSQuestion("testname.local.", const._TYPE_SRV, const._CLASS_IN)
Expand Down
5 changes: 2 additions & 3 deletions zeroconf/_dns.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,9 +579,8 @@ def add_answer(self, inp: DNSIncoming, record: DNSRecord) -> None:

def add_answer_at_time(self, record: Optional[DNSRecord], now: Union[float, int]) -> None:
"""Adds an answer if it does not expire by a certain time"""
if record is not None:
if now == 0 or not record.is_expired(now):
self.answers.append((record, now))
if record is not None and (now == 0 or not record.is_expired(now)):
self.answers.append((record, now))

def add_authorative_answer(self, record: DNSPointer) -> None:
"""Adds an authoritative answer"""
Expand Down