forked from aio-libs/aiodns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
executable file
·160 lines (129 loc) · 5.29 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
#!/usr/bin/env python
import asyncio
import ipaddress
import unittest
import socket
import sys
import aiodns
class DNSTest(unittest.TestCase):
def setUp(self):
self.loop = asyncio.new_event_loop()
self.addCleanup(self.loop.close)
self.resolver = aiodns.DNSResolver(loop=self.loop, timeout=5.0)
self.resolver.nameservers = ['8.8.8.8']
def tearDown(self):
self.resolver = None
def test_query_a(self):
f = self.resolver.query('google.com', 'A')
result = self.loop.run_until_complete(f)
def test_query_async_await(self):
async def f():
return await self.resolver.query('google.com', 'A')
result = self.loop.run_until_complete(f())
self.assertTrue(result)
def test_query_a_bad(self):
f = self.resolver.query('hgf8g2od29hdohid.com', 'A')
try:
self.loop.run_until_complete(f)
except aiodns.error.DNSError as e:
self.assertEqual(e.args[0], aiodns.error.ARES_ENOTFOUND)
def test_query_aaaa(self):
f = self.resolver.query('ipv6.google.com', 'AAAA')
result = self.loop.run_until_complete(f)
self.assertTrue(result)
def test_query_cname(self):
f = self.resolver.query('www.amazon.com', 'CNAME')
result = self.loop.run_until_complete(f)
self.assertTrue(result)
def test_query_mx(self):
f = self.resolver.query('google.com', 'MX')
result = self.loop.run_until_complete(f)
self.assertTrue(result)
def test_query_ns(self):
f = self.resolver.query('google.com', 'NS')
result = self.loop.run_until_complete(f)
self.assertTrue(result)
def test_query_txt(self):
f = self.resolver.query('google.com', 'TXT')
result = self.loop.run_until_complete(f)
self.assertTrue(result)
def test_query_soa(self):
f = self.resolver.query('google.com', 'SOA')
result = self.loop.run_until_complete(f)
self.assertTrue(result)
def test_query_srv(self):
f = self.resolver.query('_xmpp-server._tcp.jabber.org', 'SRV')
result = self.loop.run_until_complete(f)
self.assertTrue(result)
def test_query_naptr(self):
f = self.resolver.query('sip2sip.info', 'NAPTR')
result = self.loop.run_until_complete(f)
self.assertTrue(result)
def test_query_ptr(self):
ip = '8.8.8.8'
f = self.resolver.query(ipaddress.ip_address(ip).reverse_pointer, 'PTR')
result = self.loop.run_until_complete(f)
self.assertTrue(result)
def test_query_bad_type(self):
self.assertRaises(ValueError, self.resolver.query, 'google.com', 'XXX')
def test_query_txt_chaos(self):
self.resolver = aiodns.DNSResolver(loop=self.loop)
self.resolver.nameservers = ['1.1.1.1']
f = self.resolver.query('id.server', 'TXT', 'CHAOS')
result = self.loop.run_until_complete(f)
self.assertTrue(result)
def test_query_bad_class(self):
self.assertRaises(ValueError, self.resolver.query, 'google.com', 'A', "INVALIDCLASS")
def test_query_timeout(self):
self.resolver = aiodns.DNSResolver(timeout=0.1, loop=self.loop)
self.resolver.nameservers = ['1.2.3.4']
f = self.resolver.query('google.com', 'A')
try:
self.loop.run_until_complete(f)
except aiodns.error.DNSError as e:
self.assertEqual(e.args[0], aiodns.error.ARES_ETIMEOUT)
def test_query_cancel(self):
f = self.resolver.query('google.com', 'A')
self.resolver.cancel()
try:
self.loop.run_until_complete(f)
except aiodns.error.DNSError as e:
self.assertEqual(e.args[0], aiodns.error.ARES_ECANCELLED)
def test_future_cancel(self):
f = self.resolver.query('google.com', 'A')
f.cancel()
async def coro():
await asyncio.sleep(0.1)
await f
try:
self.loop.run_until_complete(coro())
except asyncio.CancelledError as e:
self.assertTrue(e)
def test_query_twice(self):
async def coro(self, host, qtype, n=2):
for i in range(n):
result = await self.resolver.query(host, qtype)
self.assertTrue(result)
self.loop.run_until_complete(coro(self, 'gmail.com', 'MX'))
def test_gethostbyname(self):
f = self.resolver.gethostbyname('google.com', socket.AF_INET)
result = self.loop.run_until_complete(f)
self.assertTrue(result)
def test_gethostbyaddr(self):
f = self.resolver.gethostbyaddr('127.0.0.1')
result = self.loop.run_until_complete(f)
self.assertTrue(result)
def test_gethostbyname_ipv6(self):
f = self.resolver.gethostbyname('ipv6.google.com', socket.AF_INET6)
result = self.loop.run_until_complete(f)
self.assertTrue(result)
def test_gethostbyname_bad_family(self):
f = self.resolver.gethostbyname('ipv6.google.com', -1)
with self.assertRaises(aiodns.error.DNSError):
self.loop.run_until_complete(f)
def test_query_bad_chars(self):
f = self.resolver.query('xn--cardeosapeluqueros-r0b.com', 'MX')
result = self.loop.run_until_complete(f)
self.assertTrue(result)
if __name__ == '__main__':
unittest.main(verbosity=2)