Debian Codegraph @

modules/ta_update/ta_update.test.integr/rfc5011/pydnstest/testserver.py

raw
1 import argparse
2 import itertools
3 import logging
4 import os
5 import random
6 import signal
7 import selectors
8 import socket
9 import sys
10 import threading
11 import time
12
13 import dns.message
14 import dns.rdatatype
15
16 from pydnstest import scenario, mock_client
17 from networking import InterfaceManager
18
19
20 class TestServerError(Exception):
21 pass
22
23
24 class QueryIoError(Exception):
25 pass
26
27
28 class TestServer:
29 """ This simulates UDP DNS server returning scripted or mirror DNS responses. """
30
31 RETRIES_ON_BIND = 3
32
33 def __init__(self, test_scenario, addr_family,
34 deckard_address=None, if_manager=None):
35 """ Initialize server instance. """
36 self.thread = None
37 self.srv_socks = []
38 self.client_socks = []
39 self.connections = []
40 self.active = False
41 self.active_lock = threading.Lock()
42 self.condition = threading.Condition()
43 self.scenario = test_scenario
44 self.scenario.deckard_address = deckard_address
45 self.addr_map = []
46 self.start_iface = 2
47 self.cur_iface = self.start_iface
48 self.addr_family = addr_family
49 self.undefined_answers = 0
50 self.if_manager = if_manager
51
52 def __del__(self):
53 """ Cleanup after deletion. """
54 with self.active_lock:
55 active = self.active
56 if active:
57 self.stop()
58
59 def start(self):
60 """ Synchronous start """
61 with self.active_lock:
62 if self.active:
63 raise TestServerError('TestServer already started')
64 with self.active_lock:
65 self.active = True
66
67 self._bind_sockets()
68
69 def stop(self):
70 """ Stop socket server operation. """
71 with self.active_lock:
72 self.active = False
73 if self.thread:
74 self.thread.join()
75 for conn in self.connections:
76 conn.close()
77 for srv_sock in self.srv_socks:
78 srv_sock.close()
79 for client_sock in self.client_socks:
80 client_sock.close()
81 self.client_socks = []
82 self.srv_socks = []
83 self.connections = []
84 self.scenario = None
85
86 def address(self):
87 """ Returns opened sockets list """
88 addrlist = []
89 for s in self.srv_socks:
90 addrlist.append(s.getsockname())
91 return addrlist
92
93 def handle_query(self, client):
94 """
95 Receive query from client socket and send an answer.
96
97 Returns:
98 True if client socket should be closed by caller
99 False if client socket should be kept open
100 """
101 log = logging.getLogger('pydnstest.testserver.handle_query')
102 server_addr = client.getsockname()[0]
103 query, client_addr = mock_client.recvfrom_msg(client)
104 if query is None:
105 return False
106 log.debug('server %s received query from %s: %s', server_addr, client_addr, query)
107
108 message = self.scenario.reply(query, server_addr)
109 if not message:
110 log.debug('ignoring')
111 return True
112 elif isinstance(message, scenario.DNSReplyServfail):
113 self.undefined_answers += 1
114 self.scenario.current_step.log.error(
115 'server %s has no response for question %s, answering with SERVFAIL',
116 server_addr,
117 '; '.join([str(rr) for rr in query.question]))
118 else:
119 log.debug('response: %s', message)
120
121 wire = message.to_wire()
122 wire_len = len(wire)
123 if query.payload > 0 and wire_len > query.payload:
124 log.warning('Reply length (%d B) is greater than the client\'s '
125 'reported EDNS payload (%d B). The test may fail '
126 'due to insufficient buffer size.',
127 wire_len, query.payload)
128
129 mock_client.sendto_msg(client, wire, client_addr)
130 return True
131
132 def query_io(self):
133 """ Main server process """
134 self.undefined_answers = 0
135 with self.active_lock:
136 if not self.active:
137 raise QueryIoError("Test server not active")
138 while True:
139 with self.condition:
140 self.condition.notify()
141 with self.active_lock:
142 if not self.active:
143 break
144 objects = self.srv_socks + self.connections
145 sel = selectors.DefaultSelector()
146 for obj in objects:
147 sel.register(obj, selectors.EVENT_READ)
148 items = sel.select(0.1)
149 for key, event in items:
150 sock = key.fileobj
151 if event & selectors.EVENT_READ:
152 if sock in self.srv_socks:
153 if sock.proto == socket.IPPROTO_TCP:
154 conn, _ = sock.accept()
155 self.connections.append(conn)
156 else:
157 self.handle_query(sock)
158 elif sock in self.connections:
159 if not self.handle_query(sock):
160 sock.close()
161 self.connections.remove(sock)
162 else:
163 raise QueryIoError(
164 f"[query_io] Socket IO internal error {sock.getsockname()}, exit"
165 )
166 else:
167 raise QueryIoError(f"[query_io] Socket IO error {sock.getsockname()}, exit")
168
169 def start_srv(self, address, family, proto=socket.IPPROTO_UDP):
170 """ Starts listening thread if necessary """
171 assert address
172 assert address[0] # host
173 assert address[1] # port
174 assert family
175 assert proto
176 if family == socket.AF_INET6:
177 if not socket.has_ipv6:
178 raise NotImplementedError(f"[start_srv] IPv6 is not supported by socket {socket}")
179 elif family != socket.AF_INET:
180 raise NotImplementedError(f"[start_srv] unsupported protocol family {family}")
181
182 if proto == socket.IPPROTO_TCP:
183 socktype = socket.SOCK_STREAM
184 elif proto == socket.IPPROTO_UDP:
185 socktype = socket.SOCK_DGRAM
186 else:
187 raise NotImplementedError(f"[start_srv] unsupported protocol {proto}")
188
189 if self.thread is None:
190 self.thread = threading.Thread(target=self.query_io)
191 self.thread.start()
192 with self.condition:
193 self.condition.wait()
194
195 for srv_sock in self.srv_socks:
196 if (srv_sock.family == family
197 and srv_sock.getsockname()[:2] == address
198 and srv_sock.proto == proto):
199 return
200
201 sock = socket.socket(family, socktype, proto)
202 sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
203
204 # Add address to interface when running from Deckard
205 if self.if_manager is not None:
206 if address[0] not in self.if_manager.added_addresses:
207 self.if_manager.add_address(address[0])
208
209 # A lot of addresses are added to the interface while runnning from Deckard in
210 # the small amount of time which caused ocassional hiccups while binding to them
211 # right afterwards in testing. Therefore, we retry a few times.
212 final_ex = None
213 for i in range(self.RETRIES_ON_BIND):
214 try:
215 sock.bind(address)
216 break
217 except OSError as ex:
218 # Exponential backoff
219 time.sleep((2 ** i) + random.random())
220 final_ex = ex
221 continue
222 else:
223 print(final_ex, address)
224 raise final_ex
225
226 if proto == socket.IPPROTO_TCP:
227 sock.listen(5)
228 self.srv_socks.append(sock)
229
230 def _bind_sockets(self):
231 """
232 Bind test server to port 53 on all addresses referenced by test scenario.
233 """
234 # Bind to test servers
235 for r in self.scenario.ranges:
236 for addr in r.addresses:
237 family = socket.AF_INET6 if ':' in addr else socket.AF_INET
238 self.start_srv((addr, 53), family)
239 self.start_srv((addr, 53), family, proto=socket.IPPROTO_TCP)
240
241 # Bind addresses in ad-hoc REPLYs
242 for s in self.scenario.steps:
243 if s.type == 'REPLY':
244 reply = s.data[0].message
245 for rr in itertools.chain(reply.answer,
246 reply.additional,
247 reply.question,
248 reply.authority):
249 for rd in rr:
250 if rd.rdtype == dns.rdatatype.A:
251 self.start_srv((rd.address, 53), socket.AF_INET)
252 self.start_srv((rd.address, 53), socket.AF_INET,
253 proto=socket.IPPROTO_TCP)
254 elif rd.rdtype == dns.rdatatype.AAAA:
255 self.start_srv((rd.address, 53), socket.AF_INET6)
256 self.start_srv((rd.address, 53), socket.AF_INET6,
257 proto=socket.IPPROTO_TCP)
258
259 def play(self, subject_addr):
260 self.scenario.play({'': (subject_addr, 53)})
261
262
263 def empty_test_case():
264 """
265 Return (scenario, config) pair which answers to any query on 127.0.0.10.
266 """
267 # Mirror server
268 empty_test_path = os.path.dirname(os.path.realpath(__file__)) + "/empty.rpl"
269 test_config = {'ROOT_ADDR': '127.0.0.10',
270 '_SOCKET_FAMILY': socket.AF_INET}
271 return scenario.parse_file(empty_test_path)[0], test_config
272
273
274 def standalone_self_test():
275 """
276 Self-test code
277
278 Usage:
279 unshare -rn $PYTHON -m pydnstest.testserver --help
280 """
281 logging.basicConfig(level=logging.DEBUG)
282 argparser = argparse.ArgumentParser()
283 argparser.add_argument('--scenario', help='absolute path to test scenario',
284 required=False)
285 argparser.add_argument('--step', help='step # in the scenario (default: first)',
286 required=False, type=int)
287 args = argparser.parse_args()
288 if args.scenario:
289 test_scenario, test_config_text = scenario.parse_file(args.scenario)
290 test_config = scenario.parse_config(test_config_text, True, os.getcwd())
291 else:
292 test_scenario, test_config = empty_test_case()
293
294 if args.step:
295 for step in test_scenario.steps:
296 if step.id == args.step:
297 test_scenario.current_step = step
298 if not test_scenario.current_step:
299 raise ValueError(f'step ID {args.step} not found in scenario')
300 else:
301 test_scenario.current_step = test_scenario.steps[0]
302
303 if_manager = InterfaceManager(interface="testserver")
304 server = TestServer(test_scenario, test_config['_SOCKET_FAMILY'], if_manager=if_manager)
305 server.start()
306
307 logging.info("[==========] Mirror server running at %s", server.address())
308
309 def kill(signum, frame): # pylint: disable=unused-argument
310 logging.info("[==========] Shutdown.")
311 server.stop()
312 sys.exit(128 + signum)
313
314 signal.signal(signal.SIGINT, kill)
315 signal.signal(signal.SIGTERM, kill)
316
317 while True:
318 time.sleep(0.5)
319
320
321 if __name__ == '__main__':
322 # this is done to avoid creating global variables
323 standalone_self_test()