Skip to content

Commit 574e241

Browse files
authored
Fix equality and hash for dns records with the unique bit (#969)
1 parent e4985c7 commit 574e241

2 files changed

Lines changed: 22 additions & 7 deletions

File tree

tests/test_dns.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,21 @@ def test_dns_record_hashablity_does_not_consider_ttl():
211211
assert len(record_set) == 1
212212

213213

214+
def test_dns_record_hashablity_does_not_consider_unique():
215+
"""Test DNSRecord are hashable and unique is ignored."""
216+
217+
# Verify the unique value is not considered in the hash
218+
record1 = r.DNSAddress(
219+
'irrelevant', const._TYPE_A, const._CLASS_IN | const._CLASS_UNIQUE, const._DNS_OTHER_TTL, b'same'
220+
)
221+
record2 = r.DNSAddress('irrelevant', const._TYPE_A, const._CLASS_IN, const._DNS_OTHER_TTL, b'same')
222+
223+
assert record1.class_ == record2.class_
224+
assert record1.__hash__() == record2.__hash__()
225+
record_set = {record1, record2}
226+
assert len(record_set) == 1
227+
228+
214229
def test_dns_address_record_hashablity():
215230
"""Test DNSAddress are hashable."""
216231
address1 = r.DNSAddress('irrelevant', const._TYPE_A, const._CLASS_IN, 1, b'a')

zeroconf/_dns.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class DNSQuestion(DNSEntry):
115115

116116
def __init__(self, name: str, type_: int, class_: int) -> None:
117117
super().__init__(name, type_, class_)
118-
self._hash = hash((self.key, type_, class_))
118+
self._hash = hash((self.key, type_, self.class_))
119119

120120
def answered_by(self, rec: 'DNSRecord') -> bool:
121121
"""Returns true if the question is answered by the record"""
@@ -247,7 +247,7 @@ def __init__(
247247
super().__init__(name, type_, class_, ttl, created)
248248
self.address = address
249249
self.scope_id = scope_id
250-
self._hash = hash((self.key, type_, class_, address, scope_id))
250+
self._hash = hash((self.key, type_, self.class_, address, scope_id))
251251

252252
def write(self, out: 'DNSOutgoing') -> None:
253253
"""Used in constructing an outgoing packet"""
@@ -290,7 +290,7 @@ def __init__(
290290
super().__init__(name, type_, class_, ttl, created)
291291
self.cpu = cpu
292292
self.os = os
293-
self._hash = hash((self.key, type_, class_, cpu, os))
293+
self._hash = hash((self.key, type_, self.class_, cpu, os))
294294

295295
def write(self, out: 'DNSOutgoing') -> None:
296296
"""Used in constructing an outgoing packet"""
@@ -326,7 +326,7 @@ def __init__(
326326
) -> None:
327327
super().__init__(name, type_, class_, ttl, created)
328328
self.alias = alias
329-
self._hash = hash((self.key, type_, class_, alias))
329+
self._hash = hash((self.key, type_, self.class_, alias))
330330

331331
@property
332332
def max_size_compressed(self) -> int:
@@ -367,7 +367,7 @@ def __init__(
367367
assert isinstance(text, (bytes, type(None)))
368368
super().__init__(name, type_, class_, ttl, created)
369369
self.text = text
370-
self._hash = hash((self.key, type_, class_, text))
370+
self._hash = hash((self.key, type_, self.class_, text))
371371

372372
def write(self, out: 'DNSOutgoing') -> None:
373373
"""Used in constructing an outgoing packet"""
@@ -411,7 +411,7 @@ def __init__(
411411
self.weight = weight
412412
self.port = port
413413
self.server = server
414-
self._hash = hash((self.key, type_, class_, priority, weight, port, server))
414+
self._hash = hash((self.key, type_, self.class_, priority, weight, port, server))
415415

416416
def write(self, out: 'DNSOutgoing') -> None:
417417
"""Used in constructing an outgoing packet"""
@@ -459,7 +459,7 @@ def __init__(
459459
super().__init__(name, type_, class_, ttl, created)
460460
self.next_name = next_name
461461
self.rdtypes = rdtypes
462-
self._hash = hash((self.key, type_, class_, next_name, *self.rdtypes))
462+
self._hash = hash((self.key, type_, self.class_, next_name, *self.rdtypes))
463463

464464
def __eq__(self, other: Any) -> bool:
465465
"""Tests equality on cpu and os"""

0 commit comments

Comments
 (0)