-
Notifications
You must be signed in to change notification settings - Fork 232
Expand file tree
/
Copy pathtest_init.py
More file actions
159 lines (122 loc) · 5.23 KB
/
Copy pathtest_init.py
File metadata and controls
159 lines (122 loc) · 5.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
from __future__ import annotations
import logging
import time
from unittest.mock import patch
import pytest
import zeroconf as r
from zeroconf import Zeroconf, const
from . import _inject_responses, make_service_info
log = logging.getLogger("zeroconf")
original_logging_level = logging.NOTSET
def setup_module():
global original_logging_level
original_logging_level = log.level
log.setLevel(logging.DEBUG)
def teardown_module():
if original_logging_level != logging.NOTSET:
log.setLevel(original_logging_level)
@pytest.mark.parametrize("label_count", [12, 1000, 4000])
def test_names_with_many_labels_encode_without_error(label_count):
"""Encoding and re-parsing a deeply nested name never raises."""
out = r.DNSOutgoing(const._FLAGS_QR_RESPONSE)
out.add_question(r.DNSQuestion("z." * label_count + "local.", const._TYPE_SRV, const._CLASS_IN))
r.DNSIncoming(out.packets()[0])
def test_oversized_label_is_rejected_at_encode_time():
"""A single label above 63 bytes cannot be written (RFC 1035 section 2.3.4)."""
out = r.DNSOutgoing(const._FLAGS_QR_RESPONSE)
out.add_question(r.DNSQuestion("q" * 70 + ".local.", const._TYPE_SRV, const._CLASS_IN))
with pytest.raises(r.NamePartTooLongException):
out.packets()
def test_repeating_a_question_keeps_every_copy():
out = r.DNSOutgoing(const._FLAGS_QR_RESPONSE)
duplicate = r.DNSQuestion("twin-entry.local.", const._TYPE_SRV, const._CLASS_IN)
for _ in range(2):
out.add_question(duplicate)
assert r.DNSIncoming(out.packets()[0]).num_questions == 2
@pytest.mark.usefixtures("quick_timing")
def test_verify_name_change_with_lots_of_names():
# instantiate a zeroconf instance
zc = Zeroconf(interfaces=["127.0.0.1"])
# create a bunch of servers
type_ = "_my-service._tcp.local."
name = "a wonderful service"
server_count = 300
generate_many_hosts(zc, type_, name, server_count)
# verify that name changing works
verify_name_change(zc, type_, name, server_count)
zc.close()
def test_large_packet_exception_log_handling():
"""Verify we downgrade debug after warning."""
# instantiate a zeroconf instance
zc = Zeroconf(interfaces=["127.0.0.1"])
with (
patch("zeroconf._logger.log.warning") as mocked_log_warn,
patch("zeroconf._logger.log.debug") as mocked_log_debug,
):
# now that we have a long packet in our possession, let's verify the
# exception handling.
out = r.DNSOutgoing(const._FLAGS_QR_RESPONSE | const._FLAGS_AA)
out.data.append(b"\0" * 10000)
# mock the zeroconf logger and check for the correct logging backoff
call_counts = mocked_log_warn.call_count, mocked_log_debug.call_count
# try to send an oversized packet
zc.send(out)
assert mocked_log_warn.call_count == call_counts[0]
zc.send(out)
assert mocked_log_warn.call_count == call_counts[0]
# mock the zeroconf logger and check for the correct logging backoff
call_counts = mocked_log_warn.call_count, mocked_log_debug.call_count
# force receive on oversized packet
zc.send(out, const._MDNS_ADDR, const._MDNS_PORT)
zc.send(out, const._MDNS_ADDR, const._MDNS_PORT)
time.sleep(0.3)
r.log.debug(
"warn %d debug %d was %s",
mocked_log_warn.call_count,
mocked_log_debug.call_count,
call_counts,
)
assert mocked_log_debug.call_count > call_counts[0]
# close our zeroconf which will close the sockets
zc.close()
def verify_name_change(zc, type_, name, number_hosts):
desc = {"path": "/healthz/"}
info_service = make_service_info(type_, f"{name}.{type_}", properties=desc)
# verify name conflict
with pytest.raises(r.NonUniqueNameException):
zc.register_service(info_service)
# verify no name conflict https://tools.ietf.org/html/rfc6762#section-6.6
zc.register_service(info_service, cooperating_responders=True)
# Create a new object since allow_name_change will mutate the
# original object and then we will have the wrong service
# in the registry
info_service2 = make_service_info(type_, f"{name}.{type_}", properties=desc)
zc.register_service(info_service2, allow_name_change=True)
assert info_service2.name.split(".")[0] == f"{name}-{number_hosts + 1}"
def generate_many_hosts(zc, type_, name, number_hosts):
block_size = 25
number_hosts = int((number_hosts - 1) / block_size + 1) * block_size
out = r.DNSOutgoing(const._FLAGS_QR_RESPONSE | const._FLAGS_AA)
for i in range(1, number_hosts + 1):
next_name = name if i == 1 else f"{name}-{i}"
generate_host(out, next_name, type_)
_inject_responses(zc, [r.DNSIncoming(packet) for packet in out.packets()])
def generate_host(out, host_name, type_):
name = ".".join((host_name, type_))
out.add_answer_at_time(
r.DNSPointer(type_, const._TYPE_PTR, const._CLASS_IN, const._DNS_OTHER_TTL, name),
0,
)
out.add_answer_at_time(
r.DNSService(
type_,
const._TYPE_SRV,
const._CLASS_IN | const._CLASS_UNIQUE,
const._DNS_HOST_TTL,
0,
0,
80,
name,
),
0,
)