Skip to content

Commit 4d6dd73

Browse files
emontnemeryjstasiak
authored andcommitted
Correct broken __eq__ in child classes to DNSRecord
1 parent 37c5211 commit 4d6dd73

File tree

2 files changed

+76
-4
lines changed

2 files changed

+76
-4
lines changed

test_zeroconf.py

+44
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
""" Unit tests for zeroconf.py """
66

7+
import copy
78
import logging
89
import socket
910
import struct
@@ -125,6 +126,49 @@ def test_match_question(self):
125126
self.assertEqual(len(generated.questions), len(parsed.questions))
126127
self.assertEqual(question, parsed.questions[0])
127128

129+
def test_suppress_answer(self):
130+
query_generated = r.DNSOutgoing(r._FLAGS_QR_QUERY)
131+
question = r.DNSQuestion("testname.local.", r._TYPE_SRV, r._CLASS_IN)
132+
query_generated.add_question(question)
133+
answer1 = r.DNSService(
134+
"testname1.local.", r._TYPE_SRV, r._CLASS_IN, r._DNS_TTL, 0, 0, 80, "foo.local.")
135+
staleanswer2 = r.DNSService(
136+
"testname2.local.", r._TYPE_SRV, r._CLASS_IN, r._DNS_TTL/2, 0, 0, 80, "foo.local.")
137+
answer2 = r.DNSService(
138+
"testname2.local.", r._TYPE_SRV, r._CLASS_IN, r._DNS_TTL, 0, 0, 80, "foo.local.")
139+
query_generated.add_answer_at_time(answer1, 0)
140+
query_generated.add_answer_at_time(staleanswer2, 0)
141+
query = r.DNSIncoming(query_generated.packet())
142+
143+
# Should be suppressed
144+
response = r.DNSOutgoing(r._FLAGS_QR_RESPONSE)
145+
response.add_answer(query, answer1)
146+
assert len(response.answers) == 0
147+
148+
# Should not be suppressed, TTL in query is too short
149+
response.add_answer(query, answer2)
150+
assert len(response.answers) == 1
151+
152+
# Should not be suppressed, name is different
153+
tmp = copy.copy(answer1)
154+
tmp.name = "testname3.local."
155+
response.add_answer(query, tmp)
156+
assert len(response.answers) == 2
157+
158+
# Should not be suppressed, type is different
159+
tmp = copy.copy(answer1)
160+
tmp.type = r._TYPE_A
161+
response.add_answer(query, tmp)
162+
assert len(response.answers) == 3
163+
164+
# Should not be suppressed, class is different
165+
tmp = copy.copy(answer1)
166+
tmp.class_ = r._CLASS_NONE
167+
response.add_answer(query, tmp)
168+
assert len(response.answers) == 4
169+
170+
# ::TODO:: could add additional tests for DNSAddress, DNSHinfo, DNSPointer, DNSText, DNSService
171+
128172
def test_dns_hinfo(self):
129173
generated = r.DNSOutgoing(0)
130174
generated.add_additional_answer(

zeroconf.py

+32-4
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,10 @@ def __eq__(self, other):
417417
"""Abstract method"""
418418
raise AbstractMethodException
419419

420+
def __ne__(self, other):
421+
"""Non-equality test"""
422+
return not self.__eq__(other)
423+
420424
def suppressed_by(self, msg):
421425
"""Returns true if any answer in a message can suffice for the
422426
information held in this record."""
@@ -478,7 +482,12 @@ def write(self, out):
478482

479483
def __eq__(self, other):
480484
"""Tests equality on address"""
481-
return isinstance(other, DNSAddress) and self.address == other.address
485+
return (isinstance(other, DNSAddress) and DNSEntry.__eq__(self, other) and
486+
self.address == other.address)
487+
488+
def __ne__(self, other):
489+
"""Non-equality test"""
490+
return not self.__eq__(other)
482491

483492
def __repr__(self):
484493
"""String representation"""
@@ -510,9 +519,13 @@ def write(self, out):
510519

511520
def __eq__(self, other):
512521
"""Tests equality on cpu and os"""
513-
return (isinstance(other, DNSHinfo) and
522+
return (isinstance(other, DNSHinfo) and DNSEntry.__eq__(self, other) and
514523
self.cpu == other.cpu and self.os == other.os)
515524

525+
def __ne__(self, other):
526+
"""Non-equality test"""
527+
return not self.__eq__(other)
528+
516529
def __repr__(self):
517530
"""String representation"""
518531
return self.cpu + " " + self.os
@@ -532,7 +545,12 @@ def write(self, out):
532545

533546
def __eq__(self, other):
534547
"""Tests equality on alias"""
535-
return isinstance(other, DNSPointer) and self.alias == other.alias
548+
return (isinstance(other, DNSPointer) and DNSEntry.__eq__(self, other) and
549+
self.alias == other.alias)
550+
551+
def __ne__(self, other):
552+
"""Non-equality test"""
553+
return not self.__eq__(other)
536554

537555
def __repr__(self):
538556
"""String representation"""
@@ -554,7 +572,12 @@ def write(self, out):
554572

555573
def __eq__(self, other):
556574
"""Tests equality on text"""
557-
return isinstance(other, DNSText) and self.text == other.text
575+
return (isinstance(other, DNSText) and DNSEntry.__eq__(self, other) and
576+
self.text == other.text)
577+
578+
def __ne__(self, other):
579+
"""Non-equality test"""
580+
return not self.__eq__(other)
558581

559582
def __repr__(self):
560583
"""String representation"""
@@ -586,11 +609,16 @@ def write(self, out):
586609
def __eq__(self, other):
587610
"""Tests equality on priority, weight, port and server"""
588611
return (isinstance(other, DNSService) and
612+
DNSEntry.__eq__(self, other) and
589613
self.priority == other.priority and
590614
self.weight == other.weight and
591615
self.port == other.port and
592616
self.server == other.server)
593617

618+
def __ne__(self, other):
619+
"""Non-equality test"""
620+
return not self.__eq__(other)
621+
594622
def __repr__(self):
595623
"""String representation"""
596624
return self.to_string("%s:%s" % (self.server, self.port))

0 commit comments

Comments
 (0)