forked from uktrade/dns-rewrite-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
565 lines (443 loc) · 18.9 KB
/
test.py
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
import asyncio
import ipaddress
import socket
import struct
import unittest
from aiodnsresolver import (
RESPONSE,
QUESTION,
TYPES,
DnsRecordDoesNotExist,
DnsResponseCode,
DnsTimeout,
IPv4AddressExpiresAt,
Message,
ResourceRecord,
QuestionRecord,
Resolver,
pack,
parse,
recvfrom,
)
from dnsrewriteproxy import (
DnsProxy,
)
def async_test(func):
def wrapper(*args, **kwargs):
future = func(*args, **kwargs)
loop = asyncio.get_event_loop()
loop.run_until_complete(future)
return wrapper
class TestProxy(unittest.TestCase):
def add_async_cleanup(self, coroutine, *args):
self.addCleanup(asyncio.get_running_loop().run_until_complete, coroutine(*args))
@async_test
async def test_e2e_no_match_rule(self):
resolve, clear_cache = get_resolver(3535)
self.add_async_cleanup(clear_cache)
start = DnsProxy(get_socket=get_socket(3535))
server_task = await start()
self.add_async_cleanup(await_cancel, server_task)
with self.assertRaises(DnsResponseCode) as cm:
await resolve('www.google.com', TYPES.A)
self.assertEqual(cm.exception.args[0], 5)
@async_test
async def test_e2e_match_all(self):
resolve, clear_cache = get_resolver(3535)
self.add_async_cleanup(clear_cache)
start = DnsProxy(get_socket=get_socket(3535), rules=((r'(^.*$)', r'\1'),))
server_task = await start()
self.add_async_cleanup(await_cancel, server_task)
response = await resolve('www.google.com', TYPES.A)
self.assertEqual(type(response[0]), IPv4AddressExpiresAt)
@async_test
async def test_e2e_match_all_wrong_type(self):
resolve, clear_cache = get_resolver(3535)
self.add_async_cleanup(clear_cache)
start = DnsProxy(get_socket=get_socket(3535), rules=((r'(^.*$)', r'\1'),))
server_task = await start()
self.add_async_cleanup(await_cancel, server_task)
with self.assertRaises(DnsResponseCode) as cm:
await resolve('www.google.com', TYPES.AAAA)
self.assertEqual(cm.exception.args[0], 5)
@async_test
async def test_e2e_default_port_match_all(self):
resolve, clear_cache = get_resolver(53)
self.add_async_cleanup(clear_cache)
start = DnsProxy(rules=((r'(^.*$)', r'\1'),))
server_task = await start()
self.add_async_cleanup(await_cancel, server_task)
response = await resolve('www.google.com', TYPES.A)
self.assertEqual(type(response[0]), IPv4AddressExpiresAt)
@async_test
async def test_e2e_default_resolver_match_all_non_existing_domain(self):
resolve, clear_cache = get_resolver(53)
self.add_async_cleanup(clear_cache)
start = DnsProxy(rules=((r'(^.*$)', r'\1'),))
server_task = await start()
self.add_async_cleanup(await_cancel, server_task)
with self.assertRaises(DnsRecordDoesNotExist):
await resolve('doesnotexist.charemza.name', TYPES.A)
@async_test
async def test_e2e_default_resolver_rewrite_non_existing_to_existing(self):
resolve, clear_cache = get_resolver(53)
self.add_async_cleanup(clear_cache)
start = DnsProxy(rules=((r'^doesnotexist\.charemza\.name$', r'www.google.com'),))
server_task = await start()
self.add_async_cleanup(await_cancel, server_task)
response = await resolve('doesnotexist.charemza.name', TYPES.A)
self.assertEqual(type(response[0]), IPv4AddressExpiresAt)
@async_test
async def test_e2e_default_resolver_match_all_bad_upstream(self):
resolve, clear_cache = get_resolver(53, timeout=100)
self.add_async_cleanup(clear_cache)
start = DnsProxy(rules=((r'(^.*$)', r'\1'),), get_resolver=lambda: get_resolver(54))
server_task = await start()
self.add_async_cleanup(await_cancel, server_task)
with self.assertRaises(DnsResponseCode) as cm:
await resolve('www.google.com', TYPES.A)
self.assertEqual(cm.exception.args[0], 2)
@async_test
async def test_e2e_default_resolver_match_none_non_existing_domain(self):
resolve, clear_cache = get_resolver(53)
self.add_async_cleanup(clear_cache)
start = DnsProxy()
server_task = await start()
self.add_async_cleanup(await_cancel, server_task)
with self.assertRaises(DnsResponseCode) as cm:
await resolve('doesnotexist.charemza.name', TYPES.A)
self.assertEqual(cm.exception.args[0], 5)
@async_test
async def test_many_responses_with_small_socket_buffer_no_onward_query(self):
resolve, clear_cache = get_resolver(53)
self.add_async_cleanup(clear_cache)
start = DnsProxy(rules=((r'(^.*$)', r'\1'),), get_socket=get_small_socket,
get_resolver=get_fixed_resolver)
server_task = await start()
self.add_async_cleanup(await_cancel, server_task)
tasks = [
asyncio.create_task(resolve('www.google.com', TYPES.A))
for _ in range(0, 100000)
]
responses = await asyncio.gather(*tasks)
for response in responses:
self.assertEqual(str(response[0]), '1.2.3.4')
bing_responses = await resolve('www.bing.com', TYPES.A)
self.assertEqual(type(bing_responses[0]), IPv4AddressExpiresAt)
@async_test
async def test_many_responses_with_small_socket_buffer_onward_query(self):
start = DnsProxy(rules=((r'(^.*$)', r'\1'),), get_socket=get_small_socket)
server_task = await start()
self.add_async_cleanup(await_cancel, server_task)
async def resolve(domain):
resolve, clear_cache = get_resolver(53)
result = await resolve(domain, TYPES.A)
await clear_cache()
return result
tasks = [
asyncio.create_task(resolve('www.google.com'))
for _ in range(0, 1000)
]
responses = await asyncio.gather(*tasks)
for response in responses:
self.assertEqual(type(response[0]), IPv4AddressExpiresAt)
bing_responses = await resolve('www.bing.com')
self.assertEqual(type(bing_responses[0]), IPv4AddressExpiresAt)
@async_test
async def test_many_responses_with_regular_socket_buffer_onward_query(self):
resolve, clear_cache = get_resolver(53)
self.add_async_cleanup(clear_cache)
start = DnsProxy(rules=((r'(^.*$)', r'\1'),))
server_task = await start()
self.add_async_cleanup(await_cancel, server_task)
tasks = [
asyncio.create_task(resolve('www.google.com', TYPES.A))
for _ in range(0, 100000)
]
responses = await asyncio.gather(*tasks)
for response in responses:
self.assertEqual(type(response[0]), IPv4AddressExpiresAt)
bing_responses = await resolve('www.bing.com', TYPES.A)
self.assertEqual(type(bing_responses[0]), IPv4AddressExpiresAt)
@async_test
async def test_proxy_returns_error_from_upstream(self):
rcode = 4
async def get_response(query_data):
query = parse(query_data)
response = Message(
qid=query.qid, qr=RESPONSE, opcode=0, aa=0, tc=0, rd=0, ra=1, z=0, rcode=rcode,
qd=query.qd, an=(), ns=(), ar=(),
)
return pack(response)
stop_nameserver = await start_nameserver(54, get_response)
self.add_async_cleanup(stop_nameserver)
resolve, clear_cache = get_resolver(53)
self.add_async_cleanup(clear_cache)
start = DnsProxy(rules=((r'(^.*$)', r'\1'),), get_resolver=lambda: get_resolver(54))
server_task = await start()
self.add_async_cleanup(await_cancel, server_task)
with self.assertRaises(DnsResponseCode) as cm:
await resolve('www.google.com', TYPES.A)
self.assertEqual(cm.exception.args[0], 4)
rcode = 5
with self.assertRaises(DnsResponseCode) as cm:
await resolve('www.google.com', TYPES.A)
self.assertEqual(cm.exception.args[0], 5)
@async_test
async def test_sending_bad_messages_not_affect_later_queries_a(self):
resolve, clear_cache = get_resolver(53)
self.add_async_cleanup(clear_cache)
start = DnsProxy(rules=((r'(^.*$)', r'\1'),))
server_task = await start()
self.add_async_cleanup(await_cancel, server_task)
response = await resolve('www.google.com', TYPES.A)
self.assertEqual(type(response[0]), IPv4AddressExpiresAt)
for _ in range(0, 100000):
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.sendto(b'not-a-valid-message', ('127.0.0.1', 53))
sock.close()
tasks = [
asyncio.create_task(resolve('www.google.com', TYPES.A))
for _ in range(0, 100000)
]
responses = await asyncio.gather(*tasks)
for response in responses:
self.assertEqual(type(response[0]), IPv4AddressExpiresAt)
@async_test
async def test_sending_bad_messages_not_affect_later_queries_b(self):
resolve, clear_cache = get_resolver(53)
self.add_async_cleanup(clear_cache)
start = DnsProxy(rules=((r'(^.*$)', r'\1'),))
server_task = await start()
self.add_async_cleanup(await_cancel, server_task)
response = await resolve('www.google.com', TYPES.A)
self.assertEqual(type(response[0]), IPv4AddressExpiresAt)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
for _ in range(0, 100000):
sock.sendto(b'not-a-valid-message', ('127.0.0.1', 53))
sock.close()
tasks = [
asyncio.create_task(resolve('www.google.com', TYPES.A))
for _ in range(0, 100000)
]
responses = await asyncio.gather(*tasks)
for response in responses:
self.assertEqual(type(response[0]), IPv4AddressExpiresAt)
@async_test
async def test_sending_lots_of_good_messages_not_affect_later_queries(self):
resolve, clear_cache = get_resolver(53)
self.add_async_cleanup(clear_cache)
start = DnsProxy(rules=((r'(^.*$)', r'\1'),))
server_task = await start()
self.add_async_cleanup(await_cancel, server_task)
response = await resolve('www.google.com', TYPES.A)
self.assertEqual(type(response[0]), IPv4AddressExpiresAt)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
for i in range(0, 100000):
name = b'doesnotexist' + str(i).encode('ascii') + b'.charemza.name'
question_record = QuestionRecord(name, TYPES.A, qclass=1)
question = Message(
qid=i % 65535, qr=QUESTION, opcode=0, aa=0, tc=0, rd=0, ra=1, z=0, rcode=0,
qd=(question_record,), an=(), ns=(), ar=(),
)
sock.sendto(pack(question), ('127.0.0.1', 53))
sock.close()
response = await resolve('www.google.com', TYPES.A)
self.assertEqual(type(response[0]), IPv4AddressExpiresAt)
@async_test
async def test_sending_pointer_loop_not_affect_later_queries_c(self):
resolve, clear_cache = get_resolver(53)
self.add_async_cleanup(clear_cache)
start = DnsProxy(rules=((r'(^.*$)', r'\1'),))
server_task = await start()
self.add_async_cleanup(await_cancel, server_task)
response = await resolve('www.google.com', TYPES.A)
self.assertEqual(type(response[0]), IPv4AddressExpiresAt)
name = b'mydomain.com'
question_record = QuestionRecord(name, TYPES.A, qclass=1)
record_1 = ResourceRecord(
name=name, qtype=TYPES.A, qclass=1, ttl=0,
rdata=ipaddress.IPv4Address('123.100.124.1').packed,
)
response = Message(
qid=1, qr=RESPONSE, opcode=0, aa=0, tc=0, rd=0, ra=1, z=0, rcode=0,
qd=(question_record,), an=(record_1,), ns=(), ar=(),
)
data = pack(response)
packed_name = b''.join(
component
for label in name.split(b'.')
for component in (bytes([len(label)]), label)
) + b'\0'
occurance_1 = data.index(packed_name)
occurance_1_end = occurance_1 + len(packed_name)
occurance_2 = occurance_1_end + data[occurance_1_end:].index(packed_name)
occurance_2_end = occurance_2 + len(packed_name)
data_compressed = \
data[:occurance_2] + \
struct.pack('!H', (192 * 256) + occurance_2 + 4) + \
struct.pack('!H', (192 * 256) + occurance_2) + \
struct.pack('!H', (192 * 256) + occurance_2 + 2) + \
data[occurance_2_end:]
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.sendto(data_compressed, ('127.0.0.1', 53))
sock.close()
tasks = [
asyncio.create_task(resolve('www.google.com', TYPES.A))
for _ in range(0, 100000)
]
responses = await asyncio.gather(*tasks)
for response in responses:
self.assertEqual(type(response[0]), IPv4AddressExpiresAt)
@async_test
async def test_too_large_response_from_upstream_not_affect_later(self):
num_records = 200
async def get_response(query_data):
query = parse(query_data)
response_records = tuple(
ResourceRecord(
name=query.qd[0].name,
qtype=TYPES.A,
qclass=1,
ttl=0,
rdata=ipaddress.IPv4Address('123.100.123.' + str(i)).packed,
) for i in range(0, num_records)
)
response = Message(
qid=query.qid, qr=RESPONSE, opcode=0, aa=0, tc=0, rd=0, ra=1, z=0, rcode=0,
qd=query.qd, an=response_records, ns=(), ar=(),
)
return pack(response)
stop_nameserver = await start_nameserver(54, get_response)
self.add_async_cleanup(stop_nameserver)
resolve, clear_cache = get_resolver(53)
self.add_async_cleanup(clear_cache)
start = DnsProxy(rules=((r'(^.*$)', r'\1'),), get_resolver=lambda: get_resolver(54))
server_task = await start()
self.add_async_cleanup(await_cancel, server_task)
tasks = [
asyncio.create_task(resolve('www.google.com', TYPES.A))
for _ in range(0, 100000)
]
for task in tasks:
with self.assertRaises(DnsTimeout):
await task
num_records = 1
tasks = [
asyncio.create_task(resolve('www.google.com', TYPES.A))
for _ in range(0, 100000)
]
responses = await asyncio.gather(*tasks)
for response in responses:
self.assertEqual(str(response[0]), '123.100.123.0')
@async_test
async def test_server_response_after_cancel_returned_to_client(self):
received_request = asyncio.Event()
continue_request = asyncio.Event()
async def get_response(query_data):
query = parse(query_data)
response_record = ResourceRecord(
name=query.qd[0].name,
qtype=TYPES.A,
qclass=1,
ttl=0,
rdata=ipaddress.IPv4Address('123.100.123.1').packed,
)
response = Message(
qid=query.qid, qr=RESPONSE, opcode=0, aa=0, tc=0, rd=0, ra=1, z=0, rcode=0,
qd=query.qd, an=(response_record,), ns=(), ar=(),
)
received_request.set()
await continue_request.wait()
return pack(response)
stop_nameserver = await start_nameserver(54, get_response)
self.add_async_cleanup(stop_nameserver)
start = DnsProxy(rules=((r'(^.*$)', r'\1'),), get_resolver=lambda: get_resolver(54))
server_task = await start()
async def resolve(domain):
resolve, clear_cache = get_resolver(53)
result = await resolve(domain, TYPES.A)
await clear_cache()
return result
# Start a set of requests
tasks = [
asyncio.create_task(resolve('www.google.com'))
for _ in range(0, 100)
]
await received_request.wait()
# Cancel the server...
server_task.cancel()
# ... start a new request
after_cancel_task = asyncio.create_task(resolve('www.bing.com'))
# ... wait to try to ensure the request would have been received
await asyncio.sleep(0.2)
# ... then finally the upstream server continues with the processing
# of the requests received before cancellation
continue_request.set()
for response in await asyncio.gather(*tasks):
self.assertEqual(str(response[0]), '123.100.123.1')
# ... but the request started after cancellation times out
with self.assertRaises(DnsTimeout):
await after_cancel_task
def get_socket(port):
def _get_socket():
sock = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
sock.setblocking(False)
sock.bind(('', port))
return sock
return _get_socket
def get_small_socket():
# For linux, the minimum buffer size is 1024
sock = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 1024)
sock.setblocking(False)
sock.bind(('', 53))
return sock
def get_resolver(port, timeout=2.0):
async def get_nameservers(_, __):
for _ in range(0, 5):
yield (timeout, ('127.0.0.1', port))
return Resolver(get_nameservers=get_nameservers)
def get_fixed_resolver():
async def get_host(_, fqdn, qtype):
hosts = {
b'www.google.com': {
TYPES.A: IPv4AddressExpiresAt('1.2.3.4', expires_at=0),
},
}
try:
return hosts[fqdn.lower()][qtype]
except KeyError:
return None
return Resolver(get_host=get_host)
async def start_nameserver(port, get_response):
# For some tests we need to control the responses from upstream, especially in the cases
# where it's not behaving
loop = asyncio.get_event_loop()
sock = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
sock.setblocking(False)
sock.bind(('', port))
async def server():
client_tasks = []
try:
while True:
data, addr = await recvfrom(loop, [sock], 512)
client_tasks.append(asyncio.ensure_future(client_task(data, addr)))
finally:
for task in client_tasks:
task.cancel()
async def client_task(data, addr):
response = await get_response(data)
sock.sendto(response, addr)
server_task = asyncio.ensure_future(server())
async def stop():
server_task.cancel()
await asyncio.sleep(0)
sock.close()
return stop
async def await_cancel(task):
task.cancel()
try:
await task
except asyncio.CancelledError:
pass