Skip to content

Commit 254c207

Browse files
authored
Merge pull request python-zeroconf#71 from stephenrauch/improved-test-coverage
Improve test coverage, and fix issues found
2 parents 208e221 + aa1f484 commit 254c207

File tree

2 files changed

+68
-24
lines changed

2 files changed

+68
-24
lines changed

test_zeroconf.py

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,60 @@ def teardown_module():
3838
log.setLevel(original_logging_level[0])
3939

4040

41+
class TestDunder(unittest.TestCase):
42+
43+
def test_dns_text_repr(self):
44+
# There was an issue on Python 3 that prevented DNSText's repr
45+
# from working when the text was longer than 10 bytes
46+
text = DNSText('irrelevant', None, 0, 0, b'12345678901')
47+
repr(text)
48+
49+
text = DNSText('irrelevant', None, 0, 0, b'123')
50+
repr(text)
51+
52+
def test_dns_hinfo_repr_eq(self):
53+
hinfo = DNSHinfo('irrelevant', r._TYPE_HINFO, 0, 0, 'cpu', 'os')
54+
assert hinfo == hinfo
55+
repr(hinfo)
56+
57+
def test_dns_pointer_repr(self):
58+
pointer = r.DNSPointer(
59+
'irrelevant', r._TYPE_PTR, r._CLASS_IN, r._DNS_TTL, '123')
60+
repr(pointer)
61+
62+
def test_dns_address_repr(self):
63+
address = r.DNSAddress('irrelevant', r._TYPE_SOA, r._CLASS_IN, 1, b'a')
64+
repr(address)
65+
66+
def test_dns_question_repr(self):
67+
question = r.DNSQuestion(
68+
'irrelevant', r._TYPE_SRV, r._CLASS_IN | r._CLASS_UNIQUE)
69+
repr(question)
70+
assert not question != question
71+
72+
def test_dns_service_repr(self):
73+
service = r.DNSService(
74+
'irrelevant', r._TYPE_SRV, r._CLASS_IN, r._DNS_TTL, 0, 0, 80, b'a')
75+
repr(service)
76+
77+
def test_dns_record_abc(self):
78+
record = r.DNSRecord('irrelevant', r._TYPE_SRV, r._CLASS_IN, r._DNS_TTL)
79+
self.assertRaises(r.AbstractMethodException, record.__eq__, record)
80+
self.assertRaises(r.AbstractMethodException, record.write, None)
81+
82+
def test_service_info_dunder(self):
83+
type_ = "_test-srvc-type._tcp.local."
84+
name = "xxxyyy"
85+
registration_name = "%s.%s" % (name, type_)
86+
info = ServiceInfo(
87+
type_, registration_name,
88+
socket.inet_aton("10.0.1.2"), 80, 0, 0,
89+
None, "ash-2.local.")
90+
91+
assert not info != info
92+
repr(info)
93+
94+
4195
class PacketGeneration(unittest.TestCase):
4296

4397
def test_parse_own_packet_simple(self):
@@ -364,7 +418,6 @@ def test_integration_with_subtype_and_listener(self):
364418
try:
365419
service_types = ZeroconfServiceTypes.find(
366420
interfaces=['127.0.0.1'], timeout=0.5)
367-
# print(service_types)
368421
assert discovery_type in service_types
369422
service_types = ZeroconfServiceTypes.find(
370423
zc=zeroconf_registrar, timeout=0.5)
@@ -395,8 +448,9 @@ def add_service(self, zeroconf, type, name):
395448
def remove_service(self, zeroconf, type, name):
396449
service_removed.set()
397450

451+
listener = MyListener()
398452
zeroconf_browser = Zeroconf(interfaces=['127.0.0.1'])
399-
zeroconf_browser.add_service_listener(subtype, MyListener())
453+
zeroconf_browser.add_service_listener(subtype, listener)
400454

401455
properties = dict(
402456
prop_none=None,
@@ -445,6 +499,7 @@ def remove_service(self, zeroconf, type, name):
445499
assert service_removed.is_set()
446500
finally:
447501
zeroconf_registrar.close()
502+
zeroconf_browser.remove_service_listener(listener)
448503
zeroconf_browser.close()
449504

450505

@@ -482,10 +537,3 @@ def on_service_state_change(zeroconf, service_type, state_change, name):
482537
zeroconf_registrar.close()
483538
browser.cancel()
484539
zeroconf_browser.close()
485-
486-
487-
def test_dnstext_repr_works():
488-
# There was an issue on Python 3 that prevented DNSText's repr
489-
# from working when the text was longer than 10 bytes
490-
text = DNSText('irrelevant', None, 0, 0, b'12345678901')
491-
repr(text)

zeroconf.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -409,8 +409,8 @@ def __init__(self, name, type_, class_, ttl):
409409
self.created = current_time_millis()
410410

411411
def __eq__(self, other):
412-
"""Tests equality as per DNSRecord"""
413-
return isinstance(other, DNSRecord) and DNSEntry.__eq__(self, other)
412+
"""Abstract method"""
413+
raise AbstractMethodException
414414

415415
def suppressed_by(self, msg):
416416
"""Returns true if any answer in a message can suffice for the
@@ -478,10 +478,9 @@ def __eq__(self, other):
478478
def __repr__(self):
479479
"""String representation"""
480480
try:
481-
return socket.inet_ntoa(self.address)
482-
except Exception as e: # TODO stop catching all Exceptions
483-
log.exception('Unknown error, possibly benign: %r', e)
484-
return self.address
481+
return str(socket.inet_ntoa(self.address))
482+
except Exception: # TODO stop catching all Exceptions
483+
return str(self.address)
485484

486485

487486
class DNSHinfo(DNSRecord):
@@ -773,7 +772,7 @@ class State(enum.Enum):
773772
adding_answers = 2
774773
adding_authoratives = 3
775774
adding_additionals = 4
776-
finished = 4
775+
finished = 5
777776

778777
def set_state(self, state):
779778
if self.state != state:
@@ -1489,9 +1488,7 @@ def request(self, zc, timeout):
14891488

14901489
def __eq__(self, other):
14911490
"""Tests equality of service name"""
1492-
if isinstance(other, ServiceInfo):
1493-
return other.name == self.name
1494-
return False
1491+
return isinstance(other, ServiceInfo) and other.name == self.name
14951492

14961493
def __ne__(self, other):
14971494
"""Non-equality test"""
@@ -1696,7 +1693,6 @@ def get_service_info(self, type_, name, timeout=3000):
16961693
info = ServiceInfo(type_, name)
16971694
if info.request(self, timeout):
16981695
return info
1699-
return None
17001696

17011697
def add_service_listener(self, type_, listener):
17021698
"""Adds a listener for a particular service type. This object
@@ -1963,7 +1959,7 @@ def send(self, out, addr=_MDNS_ADDR, port=_MDNS_PORT):
19631959
"""Sends an outgoing packet."""
19641960
packet = out.packet()
19651961
if len(packet) > _MAX_MSG_ABSOLUTE:
1966-
self.log_warning_once("Dropping %r over-sided packet (%d bytes) %r",
1962+
self.log_warning_once("Dropping %r over-sized packet (%d bytes) %r",
19671963
out, len(packet), packet)
19681964
return
19691965
log.debug('Sending %r (%d bytes) as %r...', out, len(packet), packet)
@@ -1972,9 +1968,9 @@ def send(self, out, addr=_MDNS_ADDR, port=_MDNS_PORT):
19721968
return
19731969
bytes_sent = s.sendto(packet, 0, (addr, port))
19741970
if bytes_sent != len(packet):
1975-
raise Error(
1976-
'Should not happen, sent %d out of %d bytes' % (
1977-
bytes_sent, len(packet)))
1971+
self.log_warning_once(
1972+
'!!! sent %d out of %d bytes to %r' % (
1973+
bytes_sent, len(packet)), s)
19781974

19791975
def close(self):
19801976
"""Ends the background threads, and prevent this instance from

0 commit comments

Comments
 (0)