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
|
#!/usr/bin/env python2
""" Tests for the DNS resolver and server """
# pylint: disable=too-many-public-methods, invalid-name
from random import randint
import socket
import sys
import time
import unittest
from dns.cache import RecordCache
from dns.classes import Class
from dns.message import Header, Message, Question
from dns.resolver import Resolver
from dns.resource import ResourceRecord, CNAMERecordData
from dns.types import Type
portnr = 5300
server = '127.0.0.1'
class TestResolver(unittest.TestCase):
"""Test cases for the resolver with caching disabled"""
def setUp(self):
self.resolv = Resolver(False, 0)
def test_solve(self):
"""Test solving some FQDN"""
host, aliases, addrs = self.resolv.gethostbyname('camilstaps.nl')
self.assertEqual(host, 'camilstaps.nl')
self.assertEqual(aliases, [])
self.assertEqual(addrs, ['84.22.111.158'])
def test_nonexistant(self):
"""Test solving a nonexistant FQDN"""
host, aliases, addrs = self.resolv.gethostbyname('nothing.ru.nl')
self.assertEqual(host, 'nothing.ru.nl')
self.assertEqual(aliases, [])
self.assertEqual(addrs, [])
class TestResolverCache(unittest.TestCase):
"""Test cases for the resolver with caching enabled"""
TTL = 3
def __init__(self, *pargs):
self.resolv = None
self.cache = None
super(TestResolverCache, self).__init__(*pargs)
def setup_resolver(self):
"""Setup a resolver with an invalid cache"""
self.resolv = Resolver(True, self.TTL)
self.cache = RecordCache(self.TTL)
self.cache.add_record(ResourceRecord(
'nothing.ru.nl', Type.CNAME, Class.IN, self.TTL,
CNAMERecordData('camilstaps.nl')))
self.resolv.cache = self.cache
def test_solve_invalid(self):
"""Test solving an invalid cached FQDN"""
self.setup_resolver()
host, aliases, addrs = self.resolv.gethostbyname('nothing.ru.nl')
self.assertEqual(host, 'camilstaps.nl')
self.assertEqual(aliases, [])
self.assertEqual(addrs, ['84.22.111.158'])
def test_solve_invalid_after_expiration(self):
"""Test solving an invalid cached FQDN after TTL expiration"""
self.setup_resolver()
time.sleep(self.TTL + 1)
host, aliases, addrs = self.resolv.gethostbyname('nothing.ru.nl')
self.assertEqual(host, 'nothing.ru.nl')
self.assertEqual(aliases, [])
self.assertEqual(addrs, [])
class TestServer(unittest.TestCase):
"""Test cases for the server"""
TIMEOUT = 5
def setUp(self):
self.resolv = Resolver(False, 0)
def do_query(self, hostname, type_=Type.A, class_=Class.IN):
"""Send a query to the server"""
ident = randint(0, 65535)
header = Header(ident, 0, 1, 0, 0, 0)
header.qr = 0
header.opcode = 0
header.rd = 1
req = Message(header, [Question(hostname, type_, class_)])
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.settimeout(self.TIMEOUT)
sock.sendto(req.to_bytes(), ('127.0.0.1', portnr))
try:
data = sock.recv(512)
resp = Message.from_bytes(data)
if resp.header.ident == ident:
return resp.answers, resp.authorities, resp.additionals
except socket.timeout:
pass
return [], [], []
# pylint: disable=too-many-arguments
def assert_match(self, rec, name=None, type_=None, class_=None, data=None):
"""Assert that a ResourceRecord matches some properties"""
self.assertTrue(
(name is None or rec.name == name) and
(type_ is None or rec.type_ == type_) and
(class_ is None or rec.class_ == class_) and
(data is None or rec.rdata.data == data))
def test_solve_auth(self):
"""Test solving a name for which we are authoritative"""
answs, auths, adds = self.do_query('cloogle.org')
self.assertEqual(answs, [])
self.assert_match(
auths[0], 'cloogle.org', Type.A, Class.IN, '84.22.111.158')
self.assertEqual(adds, [])
def test_solve_zone(self):
"""Test solving a name in our zone"""
answs, auths, adds = self.do_query('sub.cloogle.org')
self.assertEquals(answs + auths + adds, [])
def test_solve_outside(self):
"""Test solving a name outside our zone"""
for name in ['camilstaps.nl', 'cname.camilstaps.nl']:
answs, auths, adds = self.do_query(name)
_, aliases, addrs = self.resolv.gethostbyname(name)
for record in answs + auths + adds:
if record.type_ == Type.A:
self.assertIn(record.rdata.data, addrs)
elif record.type_ == Type.CNAME:
self.assertIn(record.rdata.data, aliases)
if __name__ == "__main__":
# Parse command line arguments
import argparse
parser = argparse.ArgumentParser(description="HTTP Tests")
parser.add_argument("-s", "--server", type=str, default="localhost")
parser.add_argument("-p", "--port", type=int, default=5300)
args, extra = parser.parse_known_args()
portnr = args.port
server = args.server
# Pass the extra arguments to unittest
sys.argv[1:] = extra
# Start test suite
unittest.main()
|