forked from saghul/pycares
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
executable file
·282 lines (247 loc) · 11.1 KB
/
tests.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
#!/usr/bin/env python
import select
import socket
import sys
import unittest
import pycares
class DNSTest(unittest.TestCase):
def setUp(self):
self.channel = pycares.Channel(timeout=1.0, tries=1)
def tearDown(self):
self.channel = None
def wait(self):
while True:
read_fds, write_fds = self.channel.getsock()
if not read_fds and not write_fds:
break
timeout = self.channel.timeout()
if timeout == 0.0:
self.channel.process_fd(pycares.ARES_SOCKET_BAD, pycares.ARES_SOCKET_BAD)
continue
rlist, wlist, xlist = select.select(read_fds, write_fds, [], timeout)
for fd in rlist:
self.channel.process_fd(fd, pycares.ARES_SOCKET_BAD)
for fd in wlist:
self.channel.process_fd(pycares.ARES_SOCKET_BAD, fd)
@unittest.skipIf(sys.platform == 'win32', 'skipped on Windows')
def test_gethostbyaddr(self):
self.result, self.errorno = None, None
def cb(result, errorno):
self.result, self.errorno = result, errorno
self.channel.gethostbyaddr('127.0.0.1', cb)
self.wait()
self.assertEqual(self.errorno, None)
self.assertEqual(type(self.result), pycares.ares_host_result)
@unittest.skipIf(sys.platform == 'win32', 'skipped on Windows')
def test_gethostbyaddr6(self):
self.result, self.errorno = None, None
def cb(result, errorno):
self.result, self.errorno = result, errorno
self.channel.gethostbyaddr('::1', cb)
self.wait()
self.assertEqual(self.errorno, None)
self.assertEqual(type(self.result), pycares.ares_host_result)
@unittest.skipIf(sys.platform == 'win32', 'skipped on Windows')
def test_gethostbyname(self):
self.result, self.errorno = None, None
def cb(result, errorno):
self.result, self.errorno = result, errorno
self.channel.gethostbyname('localhost', socket.AF_INET, cb)
self.wait()
self.assertEqual(self.errorno, None)
self.assertEqual(type(self.result), pycares.ares_host_result)
@unittest.skipIf(sys.platform == 'win32', 'skipped on Windows')
def test_gethostbyname_small_timeout(self):
self.result, self.errorno = None, None
def cb(result, errorno):
self.result, self.errorno = result, errorno
self.channel = pycares.Channel(timeout=0.5, tries=1)
self.channel.gethostbyname('localhost', socket.AF_INET, cb)
self.wait()
self.assertEqual(self.errorno, None)
self.assertEqual(type(self.result), pycares.ares_host_result)
@unittest.skipIf(sys.platform == 'win32', 'skipped on Windows')
def test_getnameinfo(self):
self.result, self.errorno = None, None
def cb(result, errorno):
self.result, self.errorno = result, errorno
self.channel.getnameinfo(('127.0.0.1', 80), pycares.ARES_NI_LOOKUPHOST|pycares.ARES_NI_LOOKUPSERVICE, cb)
self.wait()
self.assertEqual(self.errorno, None)
self.assertEqual(type(self.result), pycares.ares_nameinfo_result)
self.assertIn(self.result[0], ('localhost.localdomain', 'localhost'))
self.assertEqual(self.result[1], 'http')
def test_query_a(self):
self.result, self.errorno = None, None
def cb(result, errorno):
self.result, self.errorno = result, errorno
self.channel.query('google.com', pycares.QUERY_TYPE_A, cb)
self.wait()
self.assertEqual(self.errorno, None)
for r in self.result:
self.assertEqual(type(r), pycares.ares_query_simple_result)
self.assertNotEqual(r.host, None)
self.assertTrue(r.ttl >= 0)
def test_query_a_bad(self):
self.result, self.errorno = None, None
def cb(result, errorno):
self.result, self.errorno = result, errorno
self.channel.query('hgf8g2od29hdohid.com', pycares.QUERY_TYPE_A, cb)
self.wait()
self.assertEqual(self.result, None)
self.assertEqual(self.errorno, pycares.errno.ARES_ENOTFOUND)
def test_query_a_rotate(self):
self.result, self.errorno = None, None
self.errorno_count, self.count = 0, 0
def cb(result, errorno):
self.result, self.errorno = result, errorno
if errorno:
self.errorno_count += 1
self.count += 1
self.channel = pycares.Channel(timeout=1.0, tries=1, rotate=True)
self.channel.query('google.com', pycares.QUERY_TYPE_A, cb)
self.channel.query('google.com', pycares.QUERY_TYPE_A, cb)
self.channel.query('google.com', pycares.QUERY_TYPE_A, cb)
self.wait()
self.assertEqual(self.count, 3)
self.assertEqual(self.errorno_count, 0)
def test_query_aaaa(self):
self.result, self.errorno = None, None
def cb(result, errorno):
self.result, self.errorno = result, errorno
self.channel.query('ipv6.google.com', pycares.QUERY_TYPE_AAAA, cb)
self.wait()
self.assertEqual(self.errorno, None)
for r in self.result:
self.assertEqual(type(r), pycares.ares_query_simple_result)
self.assertNotEqual(r.host, None)
self.assertTrue(r.ttl >= 0)
def test_query_cname(self):
self.result, self.errorno = None, None
def cb(result, errorno):
self.result, self.errorno = result, errorno
self.channel.query('livechat.ripe.net', pycares.QUERY_TYPE_CNAME, cb)
self.wait()
self.assertEqual(type(self.result), pycares.ares_query_cname_result)
self.assertEqual(self.errorno, None)
def test_query_mx(self):
self.result, self.errorno = None, None
def cb(result, errorno):
self.result, self.errorno = result, errorno
self.channel.query('google.com', pycares.QUERY_TYPE_MX, cb)
self.wait()
self.assertEqual(self.errorno, None)
for r in self.result:
self.assertEqual(type(r), pycares.ares_query_mx_result)
self.assertTrue(r.ttl >= 0)
def test_query_ns(self):
self.result, self.errorno = None, None
def cb(result, errorno):
self.result, self.errorno = result, errorno
self.channel.query('google.com', pycares.QUERY_TYPE_NS, cb)
self.wait()
self.assertEqual(self.errorno, None)
for r in self.result:
self.assertEqual(type(r), pycares.ares_query_ns_result)
def test_query_txt(self):
self.result, self.errorno = None, None
def cb(result, errorno):
self.result, self.errorno = result, errorno
self.channel.query('google.com', pycares.QUERY_TYPE_TXT, cb)
self.wait()
self.assertEqual(self.errorno, None)
for r in self.result:
self.assertEqual(type(r), pycares.ares_query_txt_result)
self.assertTrue(r.ttl >= 0)
def test_query_soa(self):
self.result, self.errorno = None, None
def cb(result, errorno):
self.result, self.errorno = result, errorno
self.channel.query('google.com', pycares.QUERY_TYPE_SOA, cb)
self.wait()
self.assertEqual(type(self.result), pycares.ares_query_soa_result)
self.assertEqual(self.errorno, None)
self.assertTrue(self.result.ttl >= 0)
def test_query_srv(self):
self.result, self.errorno = None, None
def cb(result, errorno):
self.result, self.errorno = result, errorno
self.channel.query('_xmpp-server._tcp.google.com', pycares.QUERY_TYPE_SRV, cb)
self.wait()
self.assertEqual(self.errorno, None)
for r in self.result:
self.assertEqual(type(r), pycares.ares_query_srv_result)
self.assertTrue(r.ttl >= 0)
def test_query_naptr(self):
self.result, self.errorno = None, None
def cb(result, errorno):
self.result, self.errorno = result, errorno
self.channel.query('sip2sip.info', pycares.QUERY_TYPE_NAPTR, cb)
self.wait()
self.assertEqual(self.errorno, None)
for r in self.result:
self.assertEqual(type(r), pycares.ares_query_naptr_result)
self.assertTrue(r.ttl >= 0)
def test_query_ptr(self):
self.result, self.errorno = None, None
def cb(result, errorno):
self.result, self.errorno = result, errorno
ip = '8.8.8.8'
self.channel.query(pycares.reverse_address(ip), pycares.QUERY_TYPE_PTR, cb)
self.wait()
self.assertEqual(type(self.result), pycares.ares_query_ptr_result)
self.assertEqual(self.errorno, None)
def test_query_ptr_ipv6(self):
self.result, self.errorno = None, None
def cb(result, errorno):
self.result, self.errorno = result, errorno
ip = '2001:4860:4860::8888'
self.channel.query(pycares.reverse_address(ip), pycares.QUERY_TYPE_PTR, cb)
self.wait()
self.assertEqual(type(self.result), pycares.ares_query_ptr_result)
self.assertEqual(self.errorno, None)
def test_query_cancelled(self):
self.result, self.errorno = None, None
def cb(result, errorno):
self.result, self.errorno = result, errorno
self.channel.query('google.com', pycares.QUERY_TYPE_NS, cb)
self.channel.cancel()
self.wait()
self.assertEqual(self.result, None)
self.assertEqual(self.errorno, pycares.errno.ARES_ECANCELLED)
def test_channel_destroyed(self):
self.channel.destroy()
self.assertRaises(pycares.AresError, self.channel.gethostbyname, 'google.com', socket.AF_INET, lambda *x: None)
def test_query_bad_type(self):
self.assertRaises(ValueError, self.channel.query, 'google.com', 667, lambda *x: None)
self.wait()
def test_query_timeout(self):
self.result, self.errorno = None, None
def cb(result, errorno):
self.result, self.errorno = result, errorno
self.channel.servers = ['1.2.3.4']
self.channel.query('google.com', pycares.QUERY_TYPE_A, cb)
self.wait()
self.assertEqual(self.result, None)
self.assertEqual(self.errorno, pycares.errno.ARES_ETIMEOUT)
def test_reverse_address(self):
s = '1.2.3.4'
expected = '4.3.2.1.in-addr.arpa'
self.assertEqual(pycares.reverse_address(s), expected)
s = '2607:f8b0:4010:801::1013'
expected = '3.1.0.1.0.0.0.0.0.0.0.0.0.0.0.0.1.0.8.0.0.1.0.4.0.b.8.f.7.0.6.2.ip6.arpa'
self.assertEqual(pycares.reverse_address(s), expected)
def test_channel_timeout(self):
self.result, self.errorno = None, None
def cb(result, errorno):
self.result, self.errorno = result, errorno
self.channel = pycares.Channel(timeout=0.5, tries=1)
self.channel.gethostbyname('google.com', socket.AF_INET, cb)
timeout = self.channel.timeout()
self.assertTrue(timeout > 0.0)
self.channel.cancel()
self.wait()
self.assertEqual(self.result, None)
self.assertEqual(self.errorno, pycares.errno.ARES_ECANCELLED)
if __name__ == '__main__':
unittest.main(verbosity=2)